无法仅释放一个动态分配的元素

标签 c memory error-handling

所以基本上我有一个函数,它可以得到一个大学结构并充满学生。一切正常!当大学返回主大学时,所有值都存储在主大学内。
然后,当我尝试释放它时(主要), 由于某种原因,它在 braude.students[2].name 上出现错误,没有其他人。
异常(exception)是:CtrlsValidHeapPointer(block)。
为什么它会释放所有其他动态分配的名称,但不释放这个?

university getStudentInfo(FILE * file) // Definition of the getStudentInfo function
{ // This function reads student information from a file
  // and updates a university with this information using a pointer
    int i = 1, j;
    char name[99]; // A temporary name to hold the student's name.
    university tempUni; // A temporary university to hold values
    Stud * temp = (Stud*)malloc(sizeof(Stud)); // Allocating memory
                                              //  for an array of students

    if (temp == NULL) // In case there wasn't enough space 
        Error_Msg("Couldn't allocate enough memory.");

    while (getInfo(file, name, &temp[i - 1]) == 8)
    // Using getInfo==8, because in each row, there are 8 variables to scan
    {
        temp[i - 1].name = (char*)malloc(sizeof(char)*strlen(name) + 1);
        // Allocating memory for the current element's name.

        if (temp[i - 1].name == NULL) // In case there wasn't enough space, terminate
            Error_Msg("Couldn't allocate enough memory.");
        strcpy(temp[i - 1].name, name);
        // If there was, copy the name from "name" to the current student's name.

        i++; // Increase i by one to have space in memory for one more student
        temp = (Stud*)realloc(temp, i * sizeof(Stud));
        // Realloc temp, to make more space for one more student.

        if (temp == NULL) // In case there wasn't enough space, terminate.
            Error_Msg("Couldn't allocate enough memory.");
    }

    tempUni.students = (Stud*)malloc(sizeof(temp));
    // Allocating memory for students of the university, with the size of temp

    if (tempUni.students == NULL)
        // In case there wasn't enough space, terminate.
        Error_Msg("Couldn't allocate enough memory.");

    tempUni.students->marks[5] = '\0'; // Making the last mark in the string \0
    tempUni.students = temp; // Let the temporary university array of students be temp 
    tempUni.studentCount = i - 1; // How many students
    return tempUni; // Update the pointed university to have the same values as tempUni
} 

但是当我释放 main 中动态分配的内存时,如下所示:

free(braude.students[0].name);
    free(braude.students[1].name);
            free(braude.students[2].name); // Crashes here???
            free(braude.students[3].name);
            free(braude.students);

最佳答案

这一行

tempUni.students = (Stud*)malloc(sizeof(temp));

只为一个指针分配内存。该代码并不容易理解,但也许应该是

tempUni.students = malloc(i * sizeof(Stud));

关于无法仅释放一个动态分配的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171460/

相关文章:

python - 如何确定证书验证错误

c - 链接时间 "undefined reference to "错误

c++ - 为什么在函数中声明类时要使用 const 和 &?

java - 如果 ArrayList 的大小已传递给构造函数,则添加新元素时,Java 中 ArrayList 的元素是否会在内存中重新分配?

java - Java 对象数组创建后的内存大小是多少?

tensorflow - 我在将tensorflow导入为tf时出错?

c - 在 C 中使用编写的跳转表或 switch 语句是否更快?

c - 在 Windows 上显示带有自定义按钮标题的警报?

c - eventbuffer 只能读取 8 个字节的输入?

REST API : Return error for GET request if document would change depending on query parameter?