Hi I have a quick question about a program im writing. The bold part is where im stuck. As of now my output is correct. Just wondering how to delete the memory I allocated.

The question asks:

Let us say that you want to choose how many marks to enter for the student and have the array change size accordingly. This gives you the opportunity to work with dynamic pointers.

You will have to make the following changes to your code:

Change mark within the Student struct to an integer pointer.
Ask the user from main how many marks they would like to enter
Change both the functions to have an additional parameter which is the number of marks.
For the initStudent function, dynamically allocate the marks according to the number required
Add a destructor for the Student structure (~Student). The purpose of this will be to delete any memory inside this structure allocated using new.

The run might look like the following:

How many marks are there? 5
Please enter a name: Sam
Please enter an id: 195556
Please enter a mark: 87
Please enter a mark: 98
Please enter a mark: 77
Please enter a mark: 88
Please enter a mark: 78
Student info:
Name: Sam
Id: 195556
Mark 0: 87
Mark 1: 98
Mark 2: 77
Mark 3: 88
Mark 4: 78

Code
// Filename: pointers.cpp

#include <string.h>
#include <iostream>
using namespace std;


struct Student
{
    string name;
    int id;
    int* markPtr = new int [100];
};

void initStudent(Student* ptr, int marks);   // function prototype for initialization

void printStudent(Student* ptr, int marks);   // function prototype for printing

//*********************** Main Function ************************//
int main ()
{
    Student stu;          // instantiating an STUDENT object
    Student*  stuPtr = &stu;  // defining a pointer for the object
    int marks;


    cout << "How many marks are there? ";
    cin >> marks; 

    initStudent(&stu, marks);     // initializing the object
    printStudent(&stu, marks);   // printing the object



} // end main


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

void initStudent(Student* ptr, int marks)
{
    cout << "Please enter a name: ";
    cin >> ptr->name;

    cout << "Please enter an id: ";
    cin >> ptr->id;

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

void printStudent(Student* ptr, int marks)
{
    cout << "Student info: " << endl;
    cout << "Name: " << ptr->name << endl;
    cout << "Id: " << ptr->id << endl;

    for(int i=0; i < marks; i++)
    {
        cout << "Mark " << i << ": ";
        cout << ptr->markPtr[i] << endl;
    }
}

Who wrote the code here? Is this code you have been given or did you write it? I've never seen this style of doing things. I always did it more like this...

struct Student
{
    string name;
    int id;
    int* markPtr;

    Student();
};


Student::Student()
{
    markPtr = new int [100];
}

For the destructor, you would need to add a destructor and delete the array starting at address markPtr.

    struct Student
    {
        string name;
        int id;
        int* markPtr;

        Student();
        ~Student();
    };


    Student::Student()
    {
        markPtr = new int [100];
    }


    Student::~Student()
    {
        delete [] markPtr;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.