gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:34:01 25 4
gpt4 key购买 nike

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

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

}

在此先感谢您的帮助!

最佳答案

你的两个代码块有不同的行。其中一个(较大的一个)不正确。您正在传递对 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仍然会产生错误。您也有可能在剥离代码时找出问题。

关于C - 在结构内部的指针上使用 Realloc 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com