1

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?

9769953
  • 10,344
  • 3
  • 26
  • 37
  • 5
    In the first allocation, you probably want a pointer to an int for sizeof. Or rather, use `sizeof *wagner` (and in the second, `sizeof *wagner[i]`). Also, don't cast the return value of `calloc/malloc` when using C. – 9769953 Mar 15 '19 at 11:35
  • 1
    See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for the proper way to do this instead. – Lundin Mar 15 '19 at 12:13

2 Answers2

3

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 *));
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Oh, ok thank you, works from now, but different problem occured.. In my code after some algorithm I have: podobnost=wagner[sizeofvstup1+1][sizeofvstup2+1]; And for this valgrind says: Invalid read of size 8 ==8770== at 0x40144F: main (main.c:212) ==8770== Address 0x55cb9e0 is 0 bytes after a block of size 48 alloc'd and ==8770== Invalid read of size 4 ==8770== at 0x40145F: main (main.c:212) How to fix this problem? – Martin Indrych Mar 15 '19 at 12:23
  • 1
    @MartinIndrych That's a different problem, for which you should open a separate question. – dbush Mar 15 '19 at 12:26
  • can't ask anther question so early.. :/ – Martin Indrych Mar 15 '19 at 12:42
  • @MartinIndrych Then ask when you're able. :) – dbush Mar 15 '19 at 12:42
0

As others mentioned the code uses the wrong size for the 1st allocation.

To never having to think again which size to use when allocating there is simple trick:

Given that the result of a memory allocation is always assigned to a pointer just simply request to allocate (multiples of) the size this target pointer is pointing to by doing:

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

(all other changes to the code are unrelated to this "trick")

This way the code would even survive a change from

int ** wagner;

to let's say

double ** wagner;

without any modifications.

alk
  • 69,737
  • 10
  • 105
  • 255