指针数组重新分配问题

时间:2018-12-28 18:00:47

标签: c

我建立了一个将运行学术机构的程序。在一个问题中,我要在机构中建立一个部门。条件之一是,如果我所修的课程不在部门中(根据其名称),我将部门增加一并复制数据。 我是否正确执行了放大步骤?在测试系统中,我得到一个没有解释的错误。 问题本身非常广泛,因此我希望我在总结问题方面取得了相对成功

typedef struct Deprtment
{
Course**    courses;        //a pointer to an array of pointers to 
courses
int         num_of_courses; // number of courses    
char*       depart_name;    //the name of the department (Mathematics, 
//Sceince,Art,Dance etc.)    
 }Department;
 typedef struct Course
 {
char*   course_name;    //the name of the course
float   cost;           //the cost of the course in shekels
int     registered;     //how many users registered to the course
 }Course;
Department* AddCourse(Department* dp, const Course* course)
{
int i;
Course** save;
Department* updateDP;
updateDP = (Department*)malloc(sizeof(Department));
if (!dp)
{
    updateDP->depart_name = strdup("Computer Science Department");
    updateDP->courses = (Course**)malloc(1 * sizeof(Course*));
    if (!updateDP->courses) { return NULL; }
    updateDP->courses[0] = DuplicateCourse(course);
    updateDP->num_of_courses = 1;
    return updateDP;
}
for (i = 0; i < dp->num_of_courses; i++)
{
    if (strcmp(dp->courses[i]->course_name, course->course_name) == 0)
    {
        updateDP->courses[i]->registered += course->registered;
        return updateDP;
    }
    else
        save = (Course**)realloc(dp->courses, 1*sizeof(Course*));
        updateDP->courses = save;
        updateDP->courses[dp->num_of_courses]= DuplicateCourse(course);
        updateDP->num_of_courses += 1;
}
return updateDP;

}

2 个答案:

答案 0 :(得分:1)

正确的方法应该是

else {   // use {} as we are executing multiple code lines for else statement.
    save = (Course**)realloc(dp->courses, updateDP->num_of_courses * size_extension * sizeof(Course*));
    updateDP->courses = save;
    updateDP->courses[dp->num_of_courses] = DuplicateCourse(course);
    updateDP->num_of_courses += 1;
}

realloc手册页中:

  

realloc()函数将ptr指向的存储块的大小更改为size个字节。

其中size是重新分配的内存的总大小。

您的代码将内存块大小更改为sizeof(Course*)

答案 1 :(得分:1)

  

我将部门增加一并复制数据。

重新分配没有增加1。分配是1个指针的大小。 @user10605163

    save = (Course**)realloc(dp->courses, 1*sizeof(Course*));  // **Bad**
    ...

相反,请重新分配为所需的总大小。

    // No cast needed.  No coding of _type_ needed at all.
    // Broken into steps for clarity.
    size_t array_element_size = sizeof updateDP->courses[0];
    size_t desired_array_count = updateDP->num_of_courses + 1u;
    save = realloc(dp->courses, array_element_size * desired_array_count);

    // Robust code checks for allocation success.
    if (save == NULL) {
      Handle_OutOfMemory_Somehow();
      exit(EXIT_FAILURE);
    }

    // Now update.
    updateDP->courses = save;
    updateDP->courses[dp->num_of_courses]= DuplicateCourse(course);
    updateDP->num_of_courses += 1;
    } 

else区块缺少{}

else
{ // add    
    save = (Course**)realloc(dp->courses, 1*sizeof(Course*));
    updateDP->courses = save;
    updateDP->courses[dp->num_of_courses]= DuplicateCourse(course);
    updateDP->num_of_courses += 1;
} // add