2017-12-26 41 views
1

私は小さなファイルを作成し、小さな "データベース"を作成したり、エントリの作成/削除などを行う小さなプログラムを作成しています。C構造体内のポインタに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 [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); 

} 

ありがとう!

答えて

2

この行は何ですか?

addStudent(studentFile, &database); 

run機能?ローカル変数へのポインタを取り、addStudent関数に渡される場合、私はこのコードは、この修正なしでもニックの変更で動作しないことができると思い

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

私はreallocの()を使用しようとしていますどのような基本的

addStudent(studentFile, database); 
+0

ありがとう!あなたは正しい、ポインタへのポインタ。おかげさまで、Cではまだまだ新しくなっています。 –

4

あなたは2つのコードブロックが異なる行を持っています。そのうちの1つ(大きいもの)が間違っています。あなたはstudentポインタへの逆参照を渡していますか?それは必要ではない、ちょうどポインタ自体を渡す。

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

は次のようになります。

​​

また、現実的なサイズを渡していませんが、あなたの最初のコードサンプルがしました。次の行は機能しませんか?

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

これはあなたの質問からちょうどコピーされました。私はあなたが持っているもの/試していないもの、そしてあなたにエラーを与えるものについて混乱しています。

また、今後もエラーが発生するminimal exampleをさらに提供してください。コードを削除しながら問題を把握するチャンスもあります。

+0

構造体の中にあるポインタ –

+0

' - > '演算子は' struct StudentDB * Database'構造体を既に逆参照しています。 – Nick

関連する問題