Roy Gunderson
Roy Gunderson

Reputation: 29

I don't think I am destructing dynamic memory properly

I currently am trying to create a dynamic array of the structures of students. The only problem is I am getting a lot of errors, too many to count, when I currently am trying to compile. I thought I am deleting all the memory that I am using.

The input of the program is how many students and how many grades, then I dynamically create an array accordingly to how many grades they want and how many students there are.

The output should just be print each student and grade but I don't think I should have trouble with that, the dynamic memory is the part that I don't understand.

// Filename: pointers.cpp
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;


struct Student
{
  string name;
  int id;
  int* mark;
  ~Student()
 {
   delete [] mark;
   mark = NULL;
 };
};

void initStudent(Student* ptr, int markNum, int studentNum );   // function prototype for initialization
void sayStudent(Student* ptr, int markNum);     // function prototype for printing

//*********************** Main Function ************************//
int main ()
{
  int mark, studentNum;
  Student stu;           // instantiating an STUDENT object
  Student*  stuPtr = &stu;  // defining a pointer for the object
  cout << "How many marks are there? ";
  cin >>  mark;
  cout << "How many students are there?";
  cin >> studentNum;
  Student* students = new Student[studentNum];

  initStudent(&stu,mark,studentNum);       // initializing the object
  sayStudent(&stu,mark,studentNum);       // printing the object
  delete [] students;

return 0;

} // end main

//-----------------Start of functions----------------------------//

void initStudent(Student* ptr, int markNum, int studentNum)
{  ptr -> mark = new int[markNum];
   cout << "Enter Student Name :";
   cin >> ptr -> name;
   cout << "Enter Student ID Number :";
   cin >> ptr -> id;
   for (int i = 1; i <= markNum; i++)
     {
       cout << "Please enter a mark :";
       cin >> ptr -> mark[i-1];
     }
 }

void sayStudent(Student* ptr, int markNum)
 {
   cout << "Student info:"<< endl ;
   cout << "Name: " << ptr -> name << endl;
   cout << "Id:" << ptr -> id << endl;
    for (int i = 0; i < markNum; i++)
    {
      cout << "Mark " << i  << ": " << ptr -> mark[i] << endl;

     }

 }

Upvotes: 0

Views: 77

Answers (1)

パスカル
パスカル

Reputation: 489

As the above comments say, the exceptions that you are getting are not because you have unfreed memory. All C++ compilers that I know of do not throw memory leak exceptions, you need to enable that with a command line option (in some compilers), or use another tool (such as Valgrind). You are likely just experiencing syntax errors. Here is a cleaned and fixed copy of your code (not modernized):

// Filename: pointers.cpp
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;


struct Student
{

    string name;
    int id;
    int* mark;
    int markNums;
    Student(){

        cout << "Enter Student Name :";
        cin >> name;
        cout << "Enter Student ID Number :";
        cin >> this -> id;
        cout<<"Enter number of marks :";
        cin>>markNums;

        mark = new int[markNums];
        for (int i = 0; i <= (markNums-1); i++)
        {
            cout << "Please enter a mark :";
            cin >> mark[i];
        }



    }
    ~Student()
    {
        delete [] mark;
        mark = nullptr;
    };
    void say()
    {
        cout << "Student info:"<< endl ;
        cout << "Name: " << this -> name << endl;
        cout << "Id:" << this -> id << endl;

        for (int i = 0; i <(markNums); i++)
        {
            cout << "Mark " << i  << ": " << this -> mark[i] << endl;

        }

    }

};



//*********************** Main Function ************************//
int main ()
{
    int studentNum;

    cout << "How many students are there?";
    cin >> studentNum;
    Student * stuPtr;
    stuPtr=new Student[studentNum];// initializing the object
    for (int i=0; i<=(studentNum);i++){
        (*(stuPtr+i)).say();

    }
    delete [] stuPtr;

    return 0;

} // end main

As far as I know, there are no memory leaks/other errors in this program as long as you only enter the correct types. Any wrong types will trigger a memory leak and a flow issue. I hope this solves your problems. You should still modernize this code with STL constructs, which will make your program more safe and efficient.

Upvotes: 1

Related Questions