Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t仅释放一个动态分配的元素_C_Memory_Error Handling - Fatal编程技术网

Can';t仅释放一个动态分配的元素

Can';t仅释放一个动态分配的元素,c,memory,error-handling,C,Memory,Error Handling,所以基本上我有一个函数,它可以得到一个大学结构,然后让学生们来填充它。一切都好!当大学返回主大学时,所有值都存储在主大学内。 然后,当我试图释放它时(主要是), 它在braude.students[2].name上出现了故障,原因是某些原因而不是其他原因。 例外情况是:CtrlValidHeapPointer(块)。 为什么它会释放所有其他动态分配的名称,而不是这个 university getStudentInfo(FILE * file) // Definition of the getSt

所以基本上我有一个函数,它可以得到一个大学结构,然后让学生们来填充它。一切都好!当大学返回主大学时,所有值都存储在主大学内。
然后,当我试图释放它时(主要是), 它在braude.students[2].name上出现了故障,原因是某些原因而不是其他原因。
例外情况是:CtrlValidHeapPointer(块)。
为什么它会释放所有其他动态分配的名称,而不是这个

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

这真的是你的代码吗?它几乎不可读。Stud*temp=(Stud*)malloc(sizeof(Stud));你说这是分配学生的数组,但我看到你只分配了1个Stud@YoniNewman它是为每个学生重新分配的。你的学生必须是真正的
Stud
s。我不得不做一些换行。如果你不喜欢它,请自己编辑成你喜欢的形状,不需要双向滚动(上/下和左/右)阅读。(我有一个稍大的屏幕。你的屏幕一定很大。)它分配了一个学生数组,大小为temp,这是一个学生数组。他不会有内存泄漏吗?在这一行中,他分配了内存,并在
tempUni.students=temp
您在不使用free的情况下更改它,
Stud*temp
是一个指针,
sizeof temp
是该指针的大小,而不是它指向的大小。@code>Stud是指向
Stud
的指针。因此
sizeof(temp)
是指向
螺柱的指针的大小。所以
malloc(sizeof(temp))
只为指针分配足够的空间,而不是为学生数组分配足够的空间。@code>tempUni
temp
的作用是什么?为什么他们俩都是临时工?