指针传染媒介与C的动态记忆分配的

时间:2016-11-03 04:00:47

标签: c pointers recursion vector dynamic-memory-allocation

我正在学习C中指针的向量作为赋值的一部分。然而,我的代码的一部分没有运行,我不明白为什么。 我已经包含了下面的完整代码,尽管我的问题特别在于 selectApproved 函数。

我只在 selectApproved 功能中遇到问题。它接收一个 Student 类型的指针向量,它是我定义的结构,其元素中有两个不同测试的等级。该函数应该递归地识别已经获得通过结果的学生(定义为test1 + test2> = 10)并返回指针向量,其中指针向量的每个元素都包含指向的指针。学生结构。

在发生所有递归调用之后,语句 printf("位置%d \ n",位置); 之后发生错误。具体来说,我知道错误发生在这个声明中: approved_students [position] = vector [position];

我不明白为什么会发生这种错误。在所有递归调用发生之后,我动态分配指针向量以准确保存通过的学生数量: approved_students =(学生**)malloc((*通过)* sizeof(学生*)) 。据我所知,approved_student应该具有"插槽的数量" as *通过(在这种情况下是4个插槽)。 我甚至会在if语句 if(num_students == 0)中处理没有学生被批准的事件。

我知道我的递归功能运行良好,因为 printf("位置%d \ n",位置); 打印出3,这意味着4名学生有已按预期批准(学生0,1,2,3已获批准)。我正在使用的IDE(Visual Studio 2012)允许我执行 approved_students [position] = vector [position]; 。 在我看来,这句话说,例如 approved_students [position] 将包含指向位于 vector [position] 中的结构的指针。毕竟,approved_student是一个指针向量。

我真的迷失了为什么功能不起作用。有人可以帮忙吗? 我想

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

struct Studentgrade{    

    float test1;
    float test2;

};

typedef struct Studentgrade Grade;

struct student

{   

    int ID_number;
    char name[51];
    Grade *scores;

};

typedef struct student Student;

Student *createStudent(int ID_num, char name_student[], float test1_grade, float test2_grade){

    int i = 0;
    Student *newStudent = (Student *) malloc(sizeof(Student));

    if (newStudent == NULL){

        printf("Error in memory allocation\n");
        exit(1);

    }

    newStudent->scores = (Grade *) malloc(sizeof(Grade));

    if (newStudent->scores == NULL){

        printf("Error in memory allocation\n");
        exit(1);

    }

    newStudent->scores->test1 = test1_grade;
    newStudent->scores->test2 = test2_grade;

    newStudent->ID_number = ID_num;

    while(i < 51  && name_student[i] != '\0')

        newStudent->name[i] = name_student[i++];

    newStudent->name[i] = '\0';

    return newStudent;
}

Student **selectApproved(Student *vector[], int num_students, int *passed){

    int position;
    int i;
    float comparison;
    Student **approved_students = NULL;

    if(num_students == 0){

        if(*passed == 0){

            printf("No student passed\n");
            return NULL;


        }

        else{

            approved_students = (Student **) malloc( (*passed) * sizeof(Student *));

            /*for (i = 0; i < (*passed); i++)

            approved_students[i] = (Student *) malloc(sizeof(Student *));*/

            if (approved_students == NULL){

                printf("Error in memory allocation\n");
                exit(1);
            }

            return approved_students;
        }
    }

    comparison = (vector[0]->scores->test1) + (vector[0]->scores->test2);

    if(comparison >= 10){

        position = *passed;
        (*passed)++;

        approved_students = selectApproved(&vector[1], num_students - 1, passed);

        printf("Position %d\n", position);

        approved_students[position] = vector[position];


    }

    else

        selectApproved(&vector[1], num_students - 1, passed);

    return approved_students;

}

int main(void){

    Student *vpstudent[12]; 
    Student **vpstudent2;
    int i = 0;
    vpstudent[0] = createStudent(444,"Rita",6.4, 7.8);
    vpstudent[1] = createStudent(111,"Rita",5.6, 8.8);
    vpstudent[2] = createStudent(999,"Rita",8.2, 7.1);
    vpstudent[3] = createStudent(555,"Dina",2.7, 6.4);
    vpstudent[4] = createStudent(777,"Lana",9.5, 5.3);
    vpstudent[5] = createStudent(666,"Tais",2.2, 2.8);
    vpstudent[6] = createStudent(222,"Cris",3.1, 4.2);
    vpstudent[7] = createStudent(333,"Vera",4.4, 5.4);
    vpstudent[8] = createStudent(888,"Cris",4.4, 5.4);
    vpstudent[9] = createStudent(303,"Vera",4.4, 5.4);
    vpstudent[10] = createStudent(101,"Cris",5.4, 3.4);
    vpstudent[11] = createStudent(202,"Vera",1.4, 8.4);

    vpstudent2 = selectApproved(vpstudent, 12, &i);
    return 0;

}

1 个答案:

答案 0 :(得分:0)

看来你依靠selectApproved的最终递归来为你的向量分配内存。

但是,您并不能保证分配的指针实际上会返回到调用堆栈中。

&#34; if(比较&gt; = 10)&#34;的其他条件不会加载approved_students。

 if(comparison >= 10){
   ...
    approved_students = selectApproved(&vector[1], num_students - 1, passed);
   ...
} else {
    approved_students = selectApproved(&vector[1], num_students - 1, passed);
}   

return approved_students;

基于此,我会说最深层递归的学生没有通过。