-1

I'm writing a function that is giving me the following error:

/bin/sh: line 1: 15039 Bus error: 10           ( test/main.test )
make: *** [test] Error 138

I had to look up what a bus error was, and apparently it's when a function tries to access an address that doesn't exist? I've been looking through this relatively short function and can't see where that's happening.

#include <stddef.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

#include "../include/array_utils.h"

int array_new_random(int **data, int *n)
{
    int i;
    srand(time(NULL));
    if(*data == 0){
        *data = malloc(*n * sizeof(int));
    }
    for(i = 0; i < n; i++){
        *data[i] = rand();
    }
    return n;
}

And here is the function that calls it.

void test_array_new_random(void)
{
    int *buffer = NULL;
    int len = 100;
    int ret;

    t_init();
    ret = array_new_random(&buffer, &len);
    t_assert(len, ret);
    t_assert(100, len);

    free(buffer);
    t_complete();
}

And here's some of the other functions that have been called. I don't think they are as important because the code seems to be crashing before it gets to them, but I could be wrong.

void t_assert(int expected, int received)
{
    if (expected != received) {
        snprintf(buffer, sizeof(buffer), "EXPECTED %d, GOT %d.", expected, received);
        t_fail_with_err(buffer);
    }
    return;
}

void t_init()
{
    tests_status = PASS;
    test_no++;
    printf("STARTING TEST %d\n", test_no);
    return;
}

void t_complete()
{
    if (tests_status == PASS) {
        printf("PASSED TEST %d.\n", test_no);
        passed++;
    }
}

void t_fail_with_err(char *err)
{
    fprintf(stderr, "FAILED TEST %d: %s\n", test_no, err);
    tests_status = FAIL;
    tests_overall_status = FAIL;
    return;
}

Because I seem to be writing a function made to pass a test, you probably correctly guessed that this is a homework assignment.

EDIT: So, an issue was that I was using *data[i] where I should have been using (*data)[i]. However, I'm now getting this error:

/bin/sh: line 1: 15126 Segmentation fault: 11  ( test/main.test )
make: *** [test] Error 139
halfer
  • 19,824
  • 17
  • 99
  • 186
Clotex
  • 1
  • 2
  • 4
    `*data[i]` accesses out of bounds. You probably meant `(*data)[i]` – M.M Mar 13 '17 at 01:56
  • 3
    You also mix up `n` and `*n` in that function -- if the compiler didn't report this then you need to adjust your compiler settings – M.M Mar 13 '17 at 01:56
  • @M.M Ah, that makes sense. Guess I messed up the notation. I tried to use a pointer to `n`, but the compiler gave me an error, but that probably has to do with my messed up notation. I'm guessing `*data[i]` returns the value that `data[i]` points to? – Clotex Mar 13 '17 at 02:01
  • 2
    Yes; and when `i > 0` , `data[i]` doesn't point to any allocated memory – M.M Mar 13 '17 at 02:03
  • @M.M Ok, so doing that fixed the bus error, but now I'm getting a segfault... – Clotex Mar 13 '17 at 02:03
  • @M.M Wait, you said I mixed up `n` and `*n`? Don't I want the value that it's pointing to? How is what I did wrong? – Clotex Mar 13 '17 at 02:08
  • 1
    `for(i = 0; i < n; i++){ (*data)[i] = rand(); } return n;` --> `for(i = 0; i < *n; i++){ (*data)[i] = rand(); } return *n;` – BLUEPIXY Mar 13 '17 at 02:08
  • 1
    Please distill this into a [mcve]. – Nic Mar 13 '17 at 02:10
  • 1
    Check your compiler output, it will show you where you used `n` instead of `*n`. And if it doesn't , adjust your compiler settings. (I said this earlier, and you seemed to ignore it) – M.M Mar 13 '17 at 02:11
  • @BLUEPIXY Yeah, that was a silly mistake. Not sure how I missed that. Thank you! – Clotex Mar 13 '17 at 02:11
  • 1
    Note that you [only call `srand()` once](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/). – Jonathan Leffler Mar 13 '17 at 02:32
  • In relation to the remarks about how welcome homework is, the short answer is that we love homework questions here. However, if the poster has obviously not made an effort, or if they've gotten themselves in a "can't do this, won't try" then the response may range from encouragement to deletion depending on the community's assessment of the OP's prior effort. If you get just one downvote then don't worry about it. – halfer Apr 05 '17 at 17:13

1 Answers1

1

You need to change like this, to work as you expected

#include <stddef.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

#include "../include/array_utils.h"

int array_new_random(int **data, int *n)
{
    int i;
    srand(time(NULL));
    if(*data == 0){
        *data = malloc(*n * sizeof(int));
    }
    for(i = 0; i < *n; i++){   //Changed
        (*data)[i] = rand();   //Changed
    }
    return *n;                 //Changed
}
ntshetty
  • 1,293
  • 9
  • 20