Heres the piece of code I wrote to gather student details into a dynamically allocated array of structs. But when I try to delete the memory, it gives me a seg fault. I used print statements to make sure that it is reaching the delete[] part and crashing there.
the students.cpp isCode:#include <iostream> #include <string> #include "students.h" using namespace std; int main(){ unsigned int number_of_students = 0; //Gather student details cin >> number_of_students; //read student information students *student_details = new students[number_of_students]; read_students(number_of_students, student_details); delete(student_details); return 0; }
and students.h isCode:#include <iostream> #include <string> #include "students.h" using namespace std; void read_students(const unsigned int number_of_students, students *student){ for(int i = 0; i < number_of_students; i++){ cin >> (student + i) -> student_id; cin >> (student + i) -> first_name >> (student + i) -> last_name; } cout << "finished input" << endl; }
Thanks.Code:#ifndef STUDENT_H #define STUDENT_H struct student_list{ unsigned int student_id; std::string first_name; std::string last_name; }; typedef struct student_list students; void read_students(const unsigned int number_of_students, students *student); #endif



LinkBack URL
About LinkBacks



