---
title: "Dynamic memory for object and object array"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:dynamic-memory-object-array"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dynamic-memory-object-array
site: BCT Engineering
---

# Dynamic memory for object and object array

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2072 Kartik, 6 marks
- Video: [Dynamic Array of Objects in C++ | C++ Tutorial | Mr. Kishore](https://www.youtube.com/watch?v=1rzHDuzjllE) — Naresh i Technologies
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dynamic-memory-object-array
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

With suitable example, explain dynamic memory allocation for object and object array.

## Short answer

For a single object: Student *p = new Student(args); use p->method(); delete p. For array: Student *a = new Student[n]; calls default constructor n times; access a[i].method(); delete[] a. Must have default constructor for arrays. Use -> with pointer, . with array element.

## Answer

Objects can be created on the heap with `new` and destroyed with `delete`. `new` allocates memory and calls the constructor; `delete` calls the destructor and then frees memory.

#### Single object

```cpp
Student *p = new Student("Ram", 10);   // calls constructor
p->display();                          // access through pointer
delete p;                              // calls destructor, frees memory
```

#### Array of objects

```cpp
Student *s = new Student[n];           // calls default constructor n times
s[i].display();  // or (s+i)->display()
delete[] s;                            // calls destructor for each element
```

The class must have a **default constructor** because no arguments can be passed in `new T[n]`.

#### Example program

```cpp
#include <iostream>
using namespace std;

class Student
{
    int roll;
    float marks;
public:
    Student() { roll = 0; marks = 0; }
    Student(int r, float m) { roll = r; marks = m; }
    void read()
    {
        cout << "Enter roll and marks: ";
        cin >> roll >> marks;
    }
    void display() { cout << roll << "  " << marks << endl; }
    ~Student() { cout << "Object " << roll << " destroyed\n"; }
};

int main()
{
    Student *p = new Student(1, 78.5);   // one object
    p->display();
    delete p;

    int n;
    cout << "Number of students: ";
    cin >> n;
    Student *s = new Student[n];         // array of objects
    for (int i = 0; i < n; i++)
        s[i].read();
    for (int i = 0; i < n; i++)
        s[i].display();
    delete[] s;                          // destructors called for all
    return 0;
}
```

#### Points

- Use `->` with a pointer to an object and `.` with an array element.
- Use `delete[]` for arrays; plain `delete` on an array gives undefined behaviour.
- Dynamic objects live until deleted, so a memory leak occurs if we forget `delete`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What do new and delete do for an object?
   A. new only allocates; delete only calls destructor
   B. new allocates and calls the constructor; delete calls the destructor then frees memory
   C. new calls destructor; delete calls constructor
   D. Both only manipulate pointers
   Answer: B. new allocates memory and runs the constructor; delete runs the destructor and then frees the memory. (Key point: new vs delete behaviour)

2. Why must a class have a default constructor to use new Student[n]?
   A. Arrays cannot use parameterized constructors
   B. Because no arguments can be passed in new T[n]
   C. Because delete[] requires it
   D. Because the destructor needs it
   Answer: B. new T[n] cannot pass arguments, so each element is built by the default constructor. (Key point: Default constructor needed)

3. What is the correct way to free Student *s = new Student[n]?
   A. delete s;
   B. delete[] s;
   C. free(s);
   D. delete s[n];
   Answer: B. delete[] calls the destructor for every element; plain delete on an array is undefined behaviour. (Key point: Deleting an array)

4. How do you call display() on the pointer p and on an array element s[i]?
   A. p.display() and s[i]->display()
   B. p->display() and s[i].display()
   C. p->display() and s[i]->display()
   D. p.display() and s[i].display()
   Answer: B. Use -> with a pointer to an object and . with an array element, which is an object. (Key point: Member access)

## Related in Object Oriented Programming

- [Dynamic memory allocation with new and delete](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adynamic-memory-allocation) — Object Oriented Programming, chapter 3, asked 2×

## Related in other subjects

- [File system vs virtual memory; contiguous allocation](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-vs-virtual-memory-contiguous-tape) — Operating System, chapter 5
- [Array representation vs dynamic list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-vs-dynamic-list) — Data Structure and Algorithm, chapter 3
- [Array list; static or dynamic choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-static-dynamic-reason) — Data Structure and Algorithm, chapter 3
- [Static and dynamic data structures; array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-ds-array-list) — Data Structure and Algorithm, chapter 3
