Can't free dynamically allocated memory?
Aug 8, 2015 at 6:38pm UTC
I have an assignment in CS162 that I'm trying to tackle. I run the program and it works until it gets to "free(aStudent->name);". I also had to turn off _CRT_SECURE_NO_WARNINGS because of strcopy. Not sure what that's about.
The Instructions are:
1. Given the following struct declaration:
struct Student
{
char * name;
float gpa;
};
Implement the following functions:
Student * createStudent(const char name[], float gpa);
The above function creates a Student object dynamically and set name as the passed in “name” and gpa as the passed “gpa.”
bool destroyStudent(Student * aStudent);
The above function deletes the Student object pointed to by “aStudent” and set it nullptr. Remember to deallocate all
dynamically allocated memory. It returns true if the operation is successful and false if “aStudent” contains nullptr.
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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Student {
char * name;
float gpa;
};
Student * createStudent(const char name[], float gpa) {
struct Student * student = new Student;
student->name = (char *)malloc(strlen(name + 1)); //allocates proper amount of memory
strcpy(student->name, name);
student->gpa = gpa;
return student;
}
bool destroyStudent(Student*& aStudent) {
if (aStudent) { //check for null.
cout << "I made it into aStudent is null" ;
free(aStudent->name);
cout << "\nI freed student" ;
delete aStudent;
cout << "\nI deleted aStudent" ;
aStudent = nullptr ;
cout << "\nI nullptr = aStudent" ;
return true ;
}
cout << "I passed by aStudent and it looks null" ;
return false ; //if aStudent is null
}
int main() {
Student * student1 = createStudent("Bob" , 3.5);
cout << "Name is: " << student1->name << " and there GPA is: " << student1->gpa << endl;
destroyStudent(student1);
if (student1) {
cout << "Pointer is not null." << endl;
student1 = nullptr ;
}
if (!student1) {
cout << "Your pointer is null." << endl;
}
return 0;
}
Aug 8, 2015 at 7:24pm UTC
Take a look at line 18:
Line 18 has:
student->name = (char *)malloc(strlen(name + 1 ));
When it should be:
(char *)malloc(strlen(name) + 1);
Fixed code:
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Student {
char * name;
float gpa;
};
Student * createStudent(const char name[], float gpa) {
struct Student * student = new Student;
student->name = (char *)malloc(strlen(name) + 1); //allocates proper amount of memory
strcpy(student->name, name);
student->gpa = gpa;
return student;
}
bool destroyStudent(Student*& aStudent) {
if (aStudent) { //check for null.
cout << "I made it into aStudent is null" ;
free(aStudent->name);
cout << "\nI freed student" ;
delete aStudent;
cout << "\nI deleted aStudent" ;
aStudent = nullptr ;
cout << "\nI nullptr = aStudent" ;
return true ;
}
cout << "I passed by aStudent and it looks null" ;
return false ; //if aStudent is null
}
int main(int argc, char ** argv) {
Student * student1 = createStudent("Bob" , 3.5);
cout << "Name is: " << student1->name << " and there GPA is: " << student1->gpa << endl;
destroyStudent(student1);
if (student1) {
cout << "Pointer is not null." << endl;
student1 = nullptr ;
}
if (!student1) {
cout << "Your pointer is null." << endl;
}
return 0;
}
Aug 9, 2015 at 2:57am UTC
Awesome! I see how that would be a problem.
Typos are awsome.
Thanks!
Topic archived. No new replies allowed.