×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Dynamic Memory Allocation in C++ – Complete Tutorial



Last Updated on: 18th Dec 2025 16:42:46 PM

In C++, Dynamic Memory Allocation (DMA) is a technique where memory is allocated at runtime instead of compile time. This allows programs to request memory as needed, use it efficiently, and release it when no longer required.

 

memory can be allocated in two ways:

  • Static (Compile-time) memory allocation

  • Dynamic (Run-time) memory allocation

 

Dynamic Memory Allocation allows a program to request memory while the program is running, instead of deciding everything in advance.
This makes programs flexible, efficient, and scalable, especially when the amount of data is not known beforehand.

 

Definition of Dynamic Memory Allocation

Dynamic Memory Allocation is a technique in which memory is allocated and deallocated during program execution using special operators like new and delete.

 

Real-life example (Basic Idea)

Booking hotel rooms:

  • You book rooms only when guests arrive

  • You cancel (free) rooms when guests leave

Memory is used only when needed.

 

1. Why Dynamic Memory Allocation is Needed

Problems with Static Memory Allocation

  • Size must be known at compile time

  • Memory wastage if allocated more than required

  • Cannot resize memory during program execution

Benefits of Dynamic Memory Allocation

  • Memory allocated only when required

  • Efficient memory usage

  • Flexible data structures (arrays, linked lists, trees)

  • Useful for real-time and large applications

 

Real-life example

Buying food daily instead of storing food for the entire year.

 

2. Dynamic Memory Allocation using  new  Operator

The new operator:

  • Allocates memory at runtime

  • Returns the address of allocated memory

  • Stores the address in a pointer

 

Syntax

data_type *pointer = new data_type;

 

Real-life example

Renting a temporary storage locker only when needed.

 

Example 

#include <iostream>
using namespace std;

int main() {
    int *ptr = new int;

    *ptr = 40;
    cout << "Value stored dynamically: " << *ptr << endl;

    return 0;
}

 

Output

Value stored dynamically: 40

 

Short Explanation of Output

Memory is allocated at runtime, and the value is stored and accessed using a pointer.

 

3. Releasing Memory using  delete  Operator

Memory allocated using new must be released manually using   delete . If not released, it causes a memory leak.

 

Real-life example

Vacating a rented house after use so others can use it.

 

Complete Program

#include <iostream>
using namespace std;

int main() {
    int *ptr = new int;

    *ptr = 90;
    cout << "Value before deleting memory: " << *ptr << endl;

    delete ptr;
    ptr = NULL;

    return 0;
}

 

Output

Value before deleting memory: 90

 

Short Explanation of Output

The memory is released safely, preventing memory leakage.

 

4. Dynamic Allocation of Array

Dynamic memory allows creating arrays whose size is decided at runtime.

 

Real-life example

Creating a custom seating arrangement based on the number of guests.

 

Complete Program

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of students: ";
    cin >> n;

    int *arr = new int[n];

    for (int i = 0; i < n; i++) {
        arr[i] = (i + 1) * 10;
    }

    cout << "Marks: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }

    delete[] arr;

    return 0;
}

 

Output

Enter number of students: 3
Marks: 10 20 30

 

Short Explanation of Output

An array is created dynamically based on user input and deleted after use.

 

5. Dynamic Memory Allocation for Objects

Objects of a class can also be created dynamically using new.

 

Real-life example

Hiring a temporary employee for a project.

 

 

Example (Incorrect Program)

int *ptr = new int;
// memory not deleted

 

Correct Practice

delete ptr;

 

7. Difference Between Static and Dynamic Memory Allocation

Feature Static Dynamic
Allocation time Compile time Run time
Size Fixed Flexible
Memory usage Less efficient Efficient
Control Limited Full control

 

Advantages of Dynamic Memory Allocation

  • Efficient memory usage

  • Flexible data size

  • Essential for data structures

  • Better program performance

 

Disadvantages of Dynamic Memory Allocation

  • Manual memory management

  • Risk of memory leaks

  • Complex debugging

 

Summary

  • Dynamic memory is allocated at runtime

  • new allocates memory

  • delete frees memory

  • Arrays and objects can be created dynamically

  • Proper memory management is essential

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo