简体   繁体   中英

Freeing memory of dynamic struct array

its me again :D

I have the following structs:

typedef struct
{
    int day, month, year;
}date;

typedef struct
{
    char name[15];
    date birth;
    int courses;
    int *grades;
}student;

Thats how I allocated memory to each array:

printf("What is the number of students?\n");
    scanf("%d", &size); //asking for size from user and creating 'size' number of structs
    for (i = 0; i < size; i++) {
        pData[i] = malloc(sizeof(student) * size);
    }
    ........ //initializing char and birth
    for (i = 0; i < size; i++) {
        printf("\nPlease enter number of courses of student #%d:\n", i+1);
        scanf("%d", &pData[i]->courses); //allocating memory for array of grades for each student (i)
        pData[i]->grades = (int*)malloc(sizeof(int)*pData[i]->courses);
    }
    for (j = 0; j < size; j++) {
        for (i = 0; i < pData[j]->courses; i++) {
            printf("\nPlease enter grades of student #%d in course #%d\n", j+1, i+1);
            scanf("%d", &pData[j]->grades[i]);
        } //entering grades of each student

Now I am having troubles freeing the memory... Ive tried many ways but the program ends with an error everytime..

I've tried this method:

for (i = 0; i < size; i++) {
        free(pData[i].grades);
    }
    free(pData);
    pData = NULL;

Yet I still get errors...

EDIT: Thats how I declared on veriable pData:

student* pData = NULL;

Thats the function that initialize the array:

int initialize(student**);

Thats how I send the pData to the function:

size = initialize(&pData); //the function is suppose to return the size of the array.

You're not allocating space for pData properly. You do that once and assign it to *pData , not pData[i] . You have a pointer to a pointer, not an array of pointers.

printf("What is the number of students?\n");
scanf("%d", &size); //asking for size from user and creating 'size' number of structs
*pData = malloc(sizeof(student) * size);

You then need to reference it properly when reading data.

for (i = 0; i < size; i++) {
    printf("\nPlease enter number of courses of student #%d:\n", i+1);
    scanf("%d", &(*pData)[i].courses); //allocating memory for array of grades for each student (i)
    (*pData)[i].grades = (int*)malloc(sizeof(int)*(*pData)[i].courses);
}
for (j = 0; j < size; j++) {
    for (i = 0; i < (*pData)[j].courses; i++) {
        printf("\nPlease enter grades of student #%d in course #%d\n", j+1, i+1);
        scanf("%d", &(*pData)[j].grades[i]);
    } 
}

EDIT:

To further explain the syntax of how pData is being used, here are the datatype of each part of the relevant expressions.

         pData:   student **
        *pData:   student *
   (*pData)[i]:   student

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM