-1

I'm coding in C.

I'm receiving the following variable as an argument int** list.

I'm allocating the memory like this :

list = (int **)malloc(sizeof(int) * numberOfItems);

I'm looping through another list and I want to add an integer to the list variable. Here's my code :

*list[i] = i;

I'm getting the following error :

[1]    18404 segmentation fault  program

What have I done wrong ?

Sam Fischer
  • 1,442
  • 19
  • 35

2 Answers2

1

Although necessary in C++, it is not necessary (nor suggested) to cast the return of [m][c][re]alloc in ANSI C. So your first statement should be: (note the argument of the sizeof statement...)

list = malloc(sizeof(* list) * numberOfRows);//create the first level of array of pointers

Then loop through, as you have indicated in your post, allocating memory for each of the locations created in the first statement:

for(i=0;i<numOfRows;i++)
{
    list[i] = malloc(sizeof(int));
}

Note: in acknowledgement of @M.M's comment, although it is not necessary, or generally recommended (read link above) to cast return of malloc in C, the example code you provide in your original post provides one good illustration where using the cast spotlights and exposes immediately the possibility of a bug. i.e. that the cast: (int **) does not match the argument of sizeof: int.

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • The cast helps to prevent errors though. With `(int **)malloc(sizeof(int)` we can immediately see that there is a bug. And if the return type is wrong then the compiler will find the bug. If OP had written `list = malloc(sizeof(int) ` then it is less obvious. If you are going to recommend not casting then you also should recommend using the form `list = malloc(sizeof *list` , as then we can also see that there is no error. Your suggestion is the worst of the 3 options, really – M.M Apr 03 '16 at 23:41
  • @M.M - I strongly agree with `sizeof *list`. That I failed to suggest that was a mistake. However, I disagree that casting the output of `malloc()` is _generally_ a good idea in ANSI C. Though you point out a good reason in this case i.e. that the cast, would have been an immediate clue that things did not line up. But as you know, the eternal debate of casting `malloc()` and family in C is still alive and well. And I fall on the side of argument that says there is more evil casting (for C only) than not.. I did edit for your first suggestion though. Thanks. – ryyker Apr 04 '16 at 02:26
  • In my experience, type mismatch errors are far more common than the pre-C99 failure to include . – M.M Apr 04 '16 at 02:42
  • @M.M - I am still paying close attention to the arguments. Your `pre C99` clarification is interesting. I will look at that.. I have been with C99 and newer since they were available to me. – ryyker Apr 04 '16 at 02:44
0

As @FiddlingBits pointed out, my method of allocation was incorrect.

Here's the fix :

int *workList = (int *)malloc(sizeof(int) * hardworkingDwarfCount);
list = &workList;

And :

workList[i] = i;
Sam Fischer
  • 1,442
  • 19
  • 35