Dynamic Object Creation in C++
Create objects on the heap with new in C++, use them through pointers with ->, and delete them so the destructor frees the marks array each one owns.
1#include <iostream>2#include <string>3using namespace std;45class Student {6private:7 string name;8 int age;9 int* marks;10 int numSubjects;1112public:13 Student(string n, int a, int nSub) {14 name = n;15 age = a;16 numSubjects = nSub;17 marks = new int[numSubjects]; // Dynamic array in object18 }1920 void setMarks() {21 cout << "Enter marks for " << numSubjects << " subjects: ";22 for (int i = 0; i < numSubjects; i++) {23 cin >> marks[i];24 }25 }2627 void display() {28 cout << "Name: " << name << endl;29 cout << "Age: " << age << endl;30 cout << "Marks: ";31 for (int i = 0; i < numSubjects; i++) {32 cout << marks[i] << " ";33 }34 cout << endl;35 }3637 ~Student() {38 delete[] marks; // Free dynamic array39 cout << "Destructor called for " << name << endl;40 }41};4243int main() {44 // Dynamically create object45 Student* student1 = new Student("Alice", 20, 3);46 student1->setMarks();4748 cout << "\nStudent 1:" << endl;49 student1->display();5051 // Create another object52 Student* student2 = new Student("Bob", 19, 3);53 student2->setMarks();5455 cout << "\nStudent 2:" << endl;56 student2->display();5758 // Free objects59 delete student1;60 delete student2;6162 cout << "\nObjects deleted" << endl;6364 return 0;65}
Sample Output
Marks: 85 90 88
Read into Alice's heap array.
Destructor called for Alice
Printed by delete student1.
In this one
- How new creates an object and runs its constructor
- How -> calls members through a pointer
- How delete runs the destructor before freeing memory
- Why a class that owns raw memory needs careful copying
How it works
- 1
new Student("Alice", 20, 3) allocates the object and its marks array.
- 2
student1->setMarks() reads 85 90 88.
- 3
student1->display() prints the data.
- 4
The same happens for Bob.
- 5
delete student1 and delete student2 run each destructor, which frees marks.
Edge cases
Forgetting delete
Remove delete student1;
The destructor never runs, so both the Student and its marks array leak.
Copying a Student
Write Student copy = *student1;
The default copy shares the marks pointer. When both are destroyed, delete[] runs twice on the same array: a double free and undefined behavior.
What's going on
This program creates objects at run time with new and destroys them with delete. Each Student also owns a dynamic array of marks, so you see two levels of heap memory and who is responsible for each.
class Student stores a name, an age, a count of subjects, and int* marks.
The constructor sets the fields and then does marks = new int[numSubjects];. So every Student grabs its own block of heap memory the moment it is born.
setMarks() reads that many marks with cin. display() prints the name, age, and marks.
The destructor ~Student() runs delete[] marks; and prints a message. This is the object cleaning up what it allocated. Without it, every Student would leak its marks array.
In main, Student* student1 = new Student("Alice", 20, 3); does two things: it allocates memory for the Student itself on the heap, then runs the constructor, which allocates the marks. You get back a pointer.
Because student1 is a pointer, members are reached with ->: student1->setMarks(). That is shorthand for (*student1).setMarks().
delete student1; reverses the steps: it runs the destructor first (freeing the marks), then frees the Student's own memory. That is why "Destructor called for Alice" appears right at that line, not at the end of main.
Why heap objects at all? A local Student s(...) dies automatically at the end of its block. A heap object lives until you delete it, which is useful when its lifetime does not match any single block, or when you want to choose the derived type at run time and hold it through a base pointer.
Two traps hide in this class. First, if you forget delete student1;, the destructor never runs and both blocks leak. Second, Student has no copy constructor, so Student copy = *student1; would copy the marks pointer, not the marks. Both objects would then delete[] the same array, which is a double free. Memory Leak Prevention shows the Rule of Three fix, and unique_ptr<Student> with a vector<int> for marks avoids both problems.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What runs first during delete student1?
Pick an answer to check it.
Question 2
Which syntax calls display on a Student* p?
Pick an answer to check it.
Questions people ask
What does -> mean?
p->m is the same as (*p).m: follow the pointer, then access the member.
When does the destructor run for a heap object?
Exactly when delete is called on it.
What is the modern alternative?
auto s = make_unique<Student>("Alice", 20, 3); and a vector<int> for marks.