【问题标题】:Freeing memory of dynamic struct array释放动态结构数组的内存
【发布时间】:2017-06-04 15:00:39
【问题描述】:

又是我 :D

我有以下结构:

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

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

这就是我为每个数组分配内存的方式:

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

现在我在释放内存时遇到了麻烦...我尝试了很多方法,但程序每次都以错误结束..

我试过这个方法:

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

但我仍然遇到错误...

编辑:这就是我在可验证 pData 上声明的方式:

student* pData = NULL;

这就是初始化数组的函数:

int initialize(student**);

这就是我将 pData 发送到函数的方式:

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

【问题讨论】:

    标签: c struct dynamic-memory-allocation


    【解决方案1】:

    您没有为pData 正确分配空间。您这样做一次并将其分配给*pData,而不是pData[i]。你有一个指向指针的指针,而不是指针数组。

    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);
    

    然后你需要在读取数据时正确引用它。

    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]);
        } 
    }
    

    编辑:

    为了进一步解释如何使用pData 的语法,这里是相关表达式每个部分的数据类型。

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

    【讨论】:

    • 它不起作用,它说“表达式必须具有指针类型”到行 (pData)[i]->grades = (int)malloc(sizeof(int) *(*pData)[i]->课程);
    • @MatanBenishty 您错过了作业左侧pData 之前的*
    • 对不起,我复制错了,我在 Pdata 之前有 * 但它不起作用,它给出了错误(如果我删除 * 它不会给出那个错误)
    • @MatanBenishty 那么你需要在你的问题中提供更多的背景信息。按照它的编写方式,上面的所有内容似乎都在一个函数中。
    • 是的。它全部在“初始化”函数中。另外,我释放内存的方式(主要)好吗?
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    相关资源
    最近更新 更多