I have a parent struct that has an array of child structs and the length of it, and within each child struct is an array of numbers and the length of it. all of the arrays are defined using pointers because i need to dynamically allocate them and change the size of the arrays using realloc(). However when i try to free the memory of the allocated arrays, i get an using uninitialized memory warning. is there a way to "tell" the compiler that the location was initialized?
First I have the struct types "parent" and "child". The parent struct stores an array of child structs(AoS), and the length of the array of childs inside them. The child struct stores an array of integers, and the length of the array of numbers inside them. This is how they look:
typedef struct {
int numberCount;
int* numberArr;
} child;
typedef struct {
int childCount;
child* childArr;
} parent;
I need to dynamically allocate these arrays using malloc() and realloc(), and I made "initParent" and "freeParent" each for the initialization and freeing memory of a given parent struct. here's the main and the function code:
void initParent(parent* inpParent) {
inpParent->childCount = 0;
inpParent->childArr = NULL;
return;
}
void freeParent(parent* inpParent) {
for (int targetChild = 0; targetChild < inpParent->childCount; targetChild++) {
free(inpParent->childArr[targetChild].numberArr);
free(inpParent->childArr);
}
return;
}
int main(void) {
parent thisParent;
void* tempPtr;
initParent(&thisParent);
//allocating an array of child structs of length 2
thisParent.childArr = (child*)malloc(sizeof(child));
tempPtr = realloc(thisParent.childArr, sizeof(child) * 2);
if (tempPtr == NULL) {
printf("realloc returned null pointer while reallocing memory for childArr\n");
return 1;
}
thisParent.childArr = (child*)tempPtr;
thisParent.childCount = 2;
//allocating an array of integers of length 3 to the first child struct
thisParent.childArr[0].numberArr = (int*)malloc(sizeof(int));
tempPtr = realloc(thisParent.childArr[0].numberArr, sizeof(int) * 3);
if (tempPtr == NULL) {
printf("realloc returned null pointer while reallocing memory for numberArr\n");
return 1;
}
thisParent.childArr[0].numberArr = (int*)tempPtr;
thisParent.childArr[0].numberCount = 3;
//allocating an array of integers of length 3 to the second child struct
thisParent.childArr[1].numberArr = (int*)malloc(sizeof(int));
tempPtr = realloc(thisParent.childArr[1].numberArr, sizeof(int) * 3);
if (tempPtr == NULL) {
printf("realloc returned null pointer while reallocing memory for numberArr\n");
return 1;
}
thisParent.childArr[1].numberArr = (int*)tempPtr;
thisParent.childArr[1].numberCount = 3;
//quick and dirty test
thisParent.childArr[0].numberArr[0] = 1;
thisParent.childArr[0].numberArr[1] = 2;
thisParent.childArr[0].numberArr[2] = 3;
thisParent.childArr[1].numberArr[0] = 4;
thisParent.childArr[1].numberArr[1] = 5;
thisParent.childArr[1].numberArr[2] = 6;
printf("%d\n", thisParent.childArr[0].numberArr[0]);
printf("%d\n", thisParent.childArr[0].numberArr[1]);
printf("%d\n", thisParent.childArr[0].numberArr[2]);
printf("%d\n", thisParent.childArr[1].numberArr[0]);
printf("%d\n", thisParent.childArr[1].numberArr[1]);
printf("%d\n", thisParent.childArr[1].numberArr[2]);
freeParent(&thisParent);
return 0;
}
when I execute this it compiles and runs without error, however visual studio gives me the warning;
Warning C6001 Using uninitialized memory '**inpParent.childArr.numberArr'.
and
Warning C6001 Using uninitialized memory '*inpParent.childArr'.
I know that the function will have all the memory locations that i'm trying to free initialized, but the compiler doesn't seem to know that it is. Is there a way to fix this warning? Should i restructure my code instead?