C - 在Struct内部的指针上使用Realloc时崩溃

时间:2017-12-26 15:09:45

标签: c struct realloc

我一直在编写一个小程序,允许用户读取文件,创建一个小型数据库"当我尝试使用

时,能够创建/删除条目等
realloc()

功能,它崩溃。

不确定我是否做错了,可能是因为我对C来说是新手。

所以,我试着这样做:

StudentDB database;
//More code in between, that does include malloc()
database->students = realloc(database->students, (database->numberOfStudents + 1) * sizeof(Student));
//It crashes when it gets to that part. 

我要做的是使用realloc()函数作为结构内部的指针。

到目前为止,这是整个计划:

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

typedef struct Lesson {
    char *name;
    int semester;
    float grade;
} Lesson;

typedef struct Student {
    char *name;
    char *surname;
    int id;
    int numberOfLessons;
    Lesson *lesson;
} Student;

typedef struct Database {
    int numberOfStudents;
    Student *student;
} StudentDB;

static int maxNameSize = 100;
static int autoclear = 1;


void addStudent(FILE *studentFile, StudentDB *database) {
    database->numberOfStudents++;
    printf("\nAdded +1 to number of students");
    database->student = realloc(&database->student, 10);
//  
//  printf("Name of the student: ");
//  scanf("%s", database.student[database.numberOfStudents].name);
}

void clear() {
    if(autoclear) {
        system("cls");
    }
}

Lesson getNextLesson(FILE *studentFile) {
    Lesson lesson;

    lesson.name = malloc(maxNameSize * sizeof(char));
    if(!lesson.name) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }
    fscanf(studentFile, "%s", lesson.name);

    fscanf(studentFile, "%d", &lesson.semester);

    fscanf(studentFile, "%f", &lesson.grade);

    printf("\n\t%s %d || %.2f\n", lesson.name, lesson.semester, lesson.grade);
    return lesson;
}

Student getNextStudent(FILE *studentFile) {
    Student student;

    student.name = malloc(maxNameSize * sizeof(char));
    if(!student.name) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }
    fscanf(studentFile, "%s", student.name);

    student.surname = malloc(maxNameSize * sizeof(char));
    if(!student.surname) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }
    fscanf(studentFile, "%s", student.surname);

    fscanf(studentFile, "%d", &student.id);

    fscanf(studentFile, "%d", &student.numberOfLessons);

    printf("%d || %s %s || %d\n", student.id, student.name, student.surname, student.numberOfLessons);

    int lesson;

    student.lesson = malloc(student.numberOfLessons * sizeof(Lesson));

    for(lesson = 0; lesson < student.numberOfLessons; lesson++) {
        student.lesson[lesson] = getNextLesson(studentFile);
    }
    return student;
}

void loadStudents() {

}

void run(FILE *studentFile, StudentDB *database) {
    int answer;
    do {
        clear();
        answer = menu();
        switch(answer) {
            case 1: {

                break;
            }
            case 2: {

                break;
            }
            case 3: {
                addStudent(studentFile, &database);
                break;
            }
            case 4: {

                break;
            }
        }
    } while(answer < 0 || answer > 9);
}

int menu() {
    int answer;
    printf("1. Load students records from file\n");
    printf("2. Save students records to file\n");
    printf("3. Add a student record\n");
    printf("4. Delete a student record by student id\n");
    printf("5. Display a student record by student id\n");
    printf("6. Display a student record by student surname\n");
    printf("7. Display all student records\n");
    printf("8. Find the lesson average for all students\n");
    printf("9. Exit\n");

    printf("Enter the number of the thing you would like to do: ");
//  scanf("%d", &answer);
    return 3;
}

void programInfo() {
    printf("\n\n====================================================\n\tProgram Info\n\n This program was created by KKosyfarinis\n\n KKosyfarinis@uth.gr\n====================================================\n\n");
}

void readData(FILE *studentFile, StudentDB *db) {
    int i;

    printf("Running the loop\n");
    for(i = 0; i < db->numberOfStudents; i++) {
        printf("=====================\n\n\tStudent #%d\n", i);
        db->student[i] = getNextStudent(studentFile);
        printf("\n\tCompleted\n\n=====================\n");
    }

    clear();
}

void saveStudents() {

}

void main() {

    system("color 02");
    system("@echo off");

    FILE *studentFile;
    StudentDB database;

    studentFile = fopen("students.txt", "r+w");

    int numberOfStudents;

    //Set the number of students
    fscanf(studentFile, "%d", &database.numberOfStudents);

    //Prints the number of students
    printf("Number of students: %d\n", database.numberOfStudents);

    //Set the memory allocation
    database.student = malloc(database.numberOfStudents * sizeof(Student));
    if(!database.student) {
        printf("The memory allocation has failed. Exiting the program!");
        exit(0);
    }   

    //Read the students
    readData(studentFile, &database);   

    programInfo();
    run(studentFile, &database);

}

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

你的两个代码块有不同的行。其中一个(较大的一个)是不正确的。你正在引用student指针?这不是必需的,只需传递指针本身。

database->student = realloc(&database->student, 10);

应该是:

database->student = realloc(database->student, 10);

您也没有传递实际尺寸,但您的第一个代码示例是。以下行不起作用吗?

database->students = realloc(database->students, (database->numberOfStudents + 1) * sizeof(Student));

这只是从您的问题中复制而来。我对你有没有尝试过什么以及哪一个给你错误感到困惑。

此外,将来会提供更多仍然会产生错误的minimal example。在剥离代码的同时,你也有机会找出问题。

答案 1 :(得分:2)

这条线怎么样?

 addStudent(studentFile, &database);
run函数中的

?指向局部变量的指针并传递给addStudent函数

void run(FILE *studentFile, StudentDB *database) {
   ...
   case 3: {
      addStudent(studentFile, &database);  // <-- get pointer to local variable

我认为即使没有经过此修改,Nick的更改也无法使用此代码

addStudent(studentFile, database);