EDIT: I substantially changed my code to resemble the actual structure of the original code (which I can’t post because I would have to write pages and pages explaining what everything is).
I’ve been struggling with this problem. I have six int arrays, ID1, ID2 and ID3, and array1, array2 and array3, where the names with the same indexes have the same length (len1, len2 and len3, respectively). The idea is that I’m re-creating them in a for loop, because the length of these arrays changes inside it. I’m doing this as follows:
/* Before entering the loop, I define these three arrays, where
len1, len2 and len3 are all equal to 100. */
int i,S,len1,len2,len3;
len1=100;
len2=100;
len3=100;
int* ID1;
int* ID2;
int* ID3;
ID1=(int*) malloc((len1)*sizeof(int));
ID2=(int*) malloc((len2)*sizeof(int));
ID3=(int*) malloc((len3)*sizeof(int));
/* Then I fill the arrays with values from a loop which is not relevant
to my problem. Let's just fill them with a simple for loop: */
for(i=0;i<len1;i++){
ID1[i]=i;
ID2[i]=i;
ID3[i]=i;
}
/* Now I enter a loop, in which I create 3 more arrays named array1, array2
and array3: */
for(S=98;S>=0;S--){
printf("ID3[99]:%d (before)\n",ID3[99]); // 1st call to printf
int* array1;
int* array2;
int* array3;
array1=(int*) malloc((len1)*sizeof(int));
array2=(int*) malloc((len2)*sizeof(int));
array3=(int*) malloc((len3)*sizeof(int));
printf("ID3[99]:%d (after)\n",ID3[99]); // 2nd call to printf
/* I do more stuff here. Here len1, len2 and len3 changes, so I have to
re-create the "ID" arrays and the ones named "array". The idea is to fill
the new "ID1", "ID2" and "ID3" arrays with the values of another set of
arrays that I filled with values from complex calculations named
"AnotherArray1", "AnotherArray2" and "AnotherArray3".
The lenghts are always > 100.*/
free(ID1);
free(ID2);
free(ID3);
free(array1);
free(array2);
free(array3);
int* ID1;
int* ID2;
int* ID3;
ID1=(int*) malloc((len1)*sizeof(int));
ID2=(int*) malloc((len2)*sizeof(int));
ID3=(int*) malloc((len3)*sizeof(int));
for(i=0;i<len1;i++){
ID1[i]=AnotherArray1[i];
}
for(i=0;i<len2;i++){
ID2[i]=AnotherArray2[i];
}
for(i=0;i<len3;i++){
ID3[i]=AnotherArray3[i];
}
/* Finally, I need to free the "AnotherArray" arrays, because in the loop
I need to create them again and do some complex calculations with the
"ID" arrays. */
free(AnotherArray1);
free(AnotherArray2);
free(AnotherArray3);
printf("ID3[99]:%d (before, after starting the loop again)\n",ID3[99]); // 3rd call to printf
}
The problem is that when I do this, the 3rd call of the printf function is different from the 1st and 2nd calls (i.e., when the loop starts again, the value of ID3 in some of its elements suddenly changes!). I really don’t know what’s going on here…any advice? If you need more details, please let me know.
/* (array is created before the loop starts) */
printf(“array[0]: %d (before)”,array[0]);
printf(“array[0]: %d (after)”,array[0]);
int* array;
array=(int*) malloc((len1)*sizeof(int));
/* Fill array with some values, and the loop begins again */
I would note that you are allocating a new chunk of memory for the array each time. Since it is a different chunk of memory, it is likely to have different (garbage) values. I suspect that it would be a good idea to free(array) before mallocing it again.
There is a code smell in there
You are declaring ID1, ID2 and ID3 a second time.
That is likely to confuse the issue.
It is effectivly a second set of variables. That may be the source of your problem.
I would note that the original allocation never gets freed.