Im trying to increase the size of a **array with realloc which I have created with malloc.
I committed the **array to a function where I would like to increase the size.
Here is the part which increase the size:
imginf->height = imginf->height * 2;
imginf->width = imginf->width * 2;
array = realloc(array, imginf->height * sizeof(d*));
for(int i = 0; i < imginf->height; i++) {
array[i] = malloc(imginf->width * sizeof(d));
}
After that, I fill the array with two for loops. Everything works fine!
In the next function I try to save that array in a file. Here I use also two for loops.
With the first for loop I walk through the height and here I get the problem.
If the loop counter is (imginf->height /2) I ran out of the array. For the width everything’s works fine.
What went wrong? Especially, why could I fill the array?
Edit
Here the function definition for saving:
void Save(char * newname, inf imginf, d **array);
d is a struct with 3 unsigned chars.
Here the definition for the realloc function:
void reallocBig(inf *imginf, d **array);
Greetz.
C is a pass-by-value language. Your
reallocBigfunction can’t modify the value of the array in the caller. You need to change it to take ad ***parameter, and “fill in” that pointer in the function:The C FAQ has a question about precisely your problem. You should also note that your current implementation leaks all of the previous entries in the array.