Error in `./a.out'

We asked to add a reference to a student record, called bestFriend. The function displayStudentRecord(student *s) should display the first name, last name, and GPA of the student just as before. Additionally, if the “best friend” reference equal to the special value NULL, you should display “<name> has no friends (how sad!)”, or something similar. But if the best friend reference is not equal to the special value NULL, display the friend’s first name (only the first name, not the friend’s entire record. You might wonder why...)

After run my code below on ubuntu with g++, the error occur like below:
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000000914140 ***
Aborted (core dumped)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
//Develop a record type called Student
struct Student{
char* firstName;
char* lastName;
char* bestFriend;
float GPA;
};
//Function to creat a student record
Student* createStudentRecord (){
	Student* s;
	s= new Student;		    
	if(s==NULL){
		return NULL;
	}
	else{
		s->firstName=NULL;
		s->lastName=NULL;
		s->bestFriend=NULL;
		s->GPA=0.0;
	}
	return s;
}
//Record information
Student* readStudentRecordFromConsole(){
    Student* st;
    st= new Student;
    st= createStudentRecord();
    if(st==NULL){
      return NULL;
    }
    
    char* a;
    char* b;
    char* c;
    a=new char[100];
    b=new char[100];
    c=new char[100];
    cin>> a;
    st->firstName=a;
    cin>> b;
    st->lastName=b;
    cin>> c;
    st->bestFriend=c;
    cin>>st->GPA;
    return st;
}

//Function to destroy a student record
void destroyStudentRecord(Student* s){
	if(s == NULL){
		return;
	}
	if(s->firstName != NULL){
		delete s-> firstName;
	}
	if(s->lastName != NULL){
		delete s-> lastName;
	}
	if(s->bestFriend != NULL){
		delete s-> lastName;
	}
	delete s;
}
//Function to display a student record
void displayStudentRecord(Student* s){
	cout<<"Record for ";
	cout<<s->firstName<<" "<<s->lastName<<endl;
	if(s->bestFriend != NULL){
  		cout<<"The best friend is: "<<s->bestFriend<<endl;
	}
	else{
		cout<<s->firstName<<" has no friend(how sad!)"<<endl;
	}
	cout<<"The GPA is: "<<s->GPA<<endl;
}

int main(){
	Student* s=new Student;
	s=createStudentRecord();
			cout<<"calling readFromConsole, enter data in correct order"<<endl;
    	s=readStudentRecordFromConsole();
    	cout<<"done"<<endl;
    	cout<<"displaying first Student record"<<endl;
    	displayStudentRecord(s);
    	cout<<"done"<<endl;

	Student* t;
    	cout<<"calling readFromConsole again, enter data in correct order"<<endl;
   	t=readStudentRecordFromConsole();
    	cout<<"done"<<endl;
      	cout<<"displaying second Student record"<<endl;
    	displayStudentRecord(t);
    	cout<<"done"<<endl;
destroyStudentRecord(s);
destroyStudentRecord(t);
return EXIT_SUCCESS;
}
http://www.cplusplus.com/forum/general/138037/
- if allocated with new[] deallocate with delete[]
- compare lines 62 and 65, you've got a copy-paste error
- new and new[] would throw an exception in case of failure, they would not return NULL
- leaks all over the place
That's cool. I just realized that.
Requirement:
• assign the second student as the best friend of the first student
• display the student records for both students (only one will have a a friend)

For the requirement, how can I make my input to be a value NULL? "If the best friend reference is not equal to the special value NULL, display the friend’s first name." cin doesn't work here.
Topic archived. No new replies allowed.