Search code examples
carrayscalloc

How to calloc a 2D array without valgrind errors?


I am working on a school project, trying to create a 2d array based on variables.

int **wagner;
wagner = (int **)calloc((sizeofvstup1 + 1), sizeof(int));
for (int i = 0; i < (sizeofvstup1 + 1); i++) {
    wagner[i] =(int *)calloc((sizeofvstup2 + 1), sizeof(int));
}

I use calloc to get 0 on every array place. But valgrind keeps telling me something like:

Invalid write of size 8 at the "wagner[i]..."

sizeofvstup1 and sizeofvstup2 are variables of the length of the array. How am I supposed to use calloc here? Tried many times to change the lines a bit but never helped... :/

What should the code look like to work properly?


Solution

  • You're not allocating the proper amount of size for the first allocation:

    wagner =(int **)calloc((sizeofvstup1+1),sizeof(int));
    

    Here you're allocating space for an array of pointers, but you're passing sizeof(int) for the element size. If an int is smaller than an int *, then you don't have enough space and you end up reading/writing past the end of the array.

    Change the element size of the allocation to sizeof(int *). Also, don't cast the return value of calloc:

    wagner = calloc((sizeofvstup1 + 1), sizeof(int *));