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

问题描述 投票:0回答:2

我一直在编写一个小程序,允许用户读取文件,创建一个小的“数据库”以及创建/删除条目等的能力。当我尝试使用

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 [email protected]\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);

}

在此先感谢您的帮助!

c struct realloc
2个回答
2
投票

这条线怎么样?

 addStudent(studentFile, &database);

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

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

我认为即使没有这种修改,尼克的改变也无法使用此代码

addStudent(studentFile, database);

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。在剥离代码的同时,你也有可能找出问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.