Search code examples
csegmentation-faultcalloc

C segfault with calloc


These two lines are giving me a segfault and I can't figure it out:

int** input;
*input = (int*)calloc(5, sizeof(int));

That's it. The way I understand this is, request memory equal to 5 ints and return the memory address. store the memory address in the value pointed to by input. What am I missing?


Solution

  • You never initialize input so referencing whatever happens to be there, maybe this is what you want

    int** input;
    input = malloc(sizeof(int*));
    *input = calloc(5, sizeof(int));