1

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?

2
  • Where exactly is this warning coming from? Commented May 4 at 13:19
  • 6
    Do not modify questions with substantial changes after there is an answer to the original question. Stack Overflow is not an interactive debugging service. It is intended to be a durable repository of questions and answers for future users. Changing a question to invalidate existing answers breaks that. If you have a new question about changed code, you could enter a new question. However, this questions and similar new ones should have a minimal reproducible example. (Incomplete sequences of codes that do not compile by themselves are not complete reproducible examples.) Commented May 4 at 13:36

1 Answer 1

3

In this code:

    for (int targetChild = 0; targetChild < inpParent->childCount; targetChild++) {
        free(inpParent->childArr[targetChild].numberArr);
        free(inpParent->childArr);
    }

it is wrong to have free(inpParent->childArr); inside the loop. In the first iteration, the memory it points to is freed. In the second iteration, inpParent->childArr[targetChild] attempts to use the already freed memory. The behavior of this is not defined.

free(inpParent->childArr); should only be executed after the loop has completed.

Sign up to request clarification or add additional context in comments.

1 Comment

I've fixed it, but still for the free(inpParent->childArr[targetChild].numberArr); the same warning persists.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.