0

I need to keep numbers of linked-lists in an array. I have followed similar questions but confused about implementation and cannot allocate the array correctly. I debugged, found, and simplified my problem: The array is always sized as 1 instead of N. As a result, I'm getting segmentation fault when try to use nodes. Simplified code:

typedef struct Node {
    int data;
    struct Node *next;
} node;

int main(int argc, char** argv)
{
    node **array;
    int N = 5;
    array = (node **)calloc(N,sizeof(node*));
    printf("Size of array = %d", sizeof(array)/sizeof(array[0]));
    return 0;
}

And the output is:

Size of array = 1

Here when I use static size for array as node *array[5] the output is:

Size of array = 5

Thanks for suggestions.

smtnkc
  • 488
  • 2
  • 9
  • 23
  • 2
    If this is [tag:c++] use the `new` operator, if it's [tag:c] do not cast the return value of `calloc()`. – Iharob Al Asimi Dec 23 '15 at 19:34
  • @DietrichEpp How did you find the duplicate. I couldn't. – Iharob Al Asimi Dec 23 '15 at 19:37
  • @iharob Thanks, it is c. I removed casting but still doesn't work. – smtnkc Dec 23 '15 at 19:38
  • @DietrichEpp It is not duplicate. – smtnkc Dec 23 '15 at 19:39
  • I did not say that it would work, I said that you should not cast and also, if it's [tag:c], do not tag as [tag:c++]. – Iharob Al Asimi Dec 23 '15 at 19:39
  • @iharob: I did a search for `[c] sizeof pointer` in a separate browser tab. I did a couple other searches first. It took me longer than I anticipated. – Dietrich Epp Dec 23 '15 at 19:39
  • @samet It is duplicate and the same question is asked over and over despite the fact that there are *more than a few* duplicates. Although since @[DietrichEpp](http://stackoverflow.com/users/82294/dietrich-epp) and I both had trouble finding it, I can see why you asked it again. – Iharob Al Asimi Dec 23 '15 at 19:40
  • 2
    I am sorry that you feel that this is not a duplicate question. However, the problem here is that you are using `sizeof` on a pointer, which points to an array returned by `malloc()`, and `sizeof` is returning the size of the pointer instead of the size of the array. This is a very common gotcha in C and it is addressed in the other question, which is why I marked this question as a duplicate. The other question has the answer you seek. – Dietrich Epp Dec 23 '15 at 19:41
  • @iharob but doesn't calloc return a void pointer? gcc would complain normally. – Aiman Al-Eryani Dec 23 '15 at 19:43
  • 1
    @AimanAl-Eryani: You are thinking of C++, which is a different language. – Dietrich Epp Dec 23 '15 at 19:45
  • 2
    No *g++* would complain because the conversion from `void *` to another pointer type requires a cast in [tag:c++] but in [tag:c++] you would use `new Node[N]` instead - "*although maybe you want to use `malloc()` because you can use `realloc()` bear in mind that `malloc()` does not call the constructor*". In [tag:c] this happens automatically and using the cast just makes the code hard to read, and makes you repeat yourself and [more issues too](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). You can avoid them, and write neater code by not casting in [tag:c]. – Iharob Al Asimi Dec 23 '15 at 19:45
  • @iharob & Dietrich: I believed what you said, but then I thought I should try and see for myself: http://i.imgur.com/ApHc6bp.png – Aiman Al-Eryani Dec 23 '15 at 19:56
  • 1
    @AimanAl-Eryani Your file is a *.cpp* file, it's automatically compiled as a [tag:c++] program. You need to understand that [tag:c] and [tag:c++] are different languages. In the beginning [tag:c++] tried to be as compatible as possible with [tag:c]'s syntax, and that is no longer the case ("*most [tag:c] programs still compilable with a [tag:c++] compiler but I think the effort to keep that feature is no longer a priority*"). You try renamnig your file *test.c* instead, does the compiler complain now? – Iharob Al Asimi Dec 23 '15 at 19:59
  • And really avoid `malloc()` in [tag:c++] programs, because `malloc()`ing [tag:c++] classes is *simply* wrong. – Iharob Al Asimi Dec 23 '15 at 20:01
  • @iharob: Aha! I realize C and C++ are different.I stand corrected. I thought the extension wouldn't matter as long as I use gcc and not g++. Thank you very much! Edit: I never really use malloc (I use new), but I will put that in mind. – Aiman Al-Eryani Dec 23 '15 at 20:02
  • 1
    In fact *gcc* automatically picks a compiler based on the file extension. You know that, *gcc* stands for GNU Compilers Collection, right? It has nothing to do with the [tag:c] language. – Iharob Al Asimi Dec 23 '15 at 20:03
  • I legitimately did not know that. Thanks! – Aiman Al-Eryani Dec 23 '15 at 20:05

0 Answers0