Dynamic memory lets a program get more memory while it runs. This helps when you don't know how much memory you need before the program starts.
When you want to store a list but don't know its size in advance.
When you need to create objects during the program based on user input.
When you want to use memory efficiently and only allocate what you need.
When working with data that changes size, like growing arrays or linked lists.
Syntax
C++
int* ptr = newint; // allocate memory for one intint* arr = newint[size]; // allocate memory for an array// When done, free memory:
delete ptr;
delete[] arr;
Use new to get memory from the heap during runtime.
Always use delete or delete[] to free memory and avoid leaks.
Examples
Allocates memory for one integer, stores 10, prints it, then frees the memory.
C++
int* p = newint; // allocate one int
*p = 10;
std::cout << *p << std::endl;
delete p;
Creates an array of 5 integers, fills it with even numbers, prints them, then frees the array.
C++
int size = 5;
int* arr = newint[size];
for(int i = 0; i < size; i++) {
arr[i] = i * 2;
}
for(int i = 0; i < size; i++) {
std::cout << arr[i] << ' ';
}
std::cout << std::endl;
delete[] arr;
Sample Program
This program asks the user how many numbers they want to store. It then creates a dynamic array of that size, fills it with numbers from 1 to n, prints them, and frees the memory.
C++
#include <iostream>
int main() {
int n;
std::cout << "Enter number of elements: ";
std::cin >> n;
int* numbers = newint[n];
for(int i = 0; i < n; i++) {
numbers[i] = i + 1;
}
std::cout << "Numbers: ";
for(int i = 0; i < n; i++) {
std::cout << numbers[i] << ' ';
}
std::cout << std::endl;
delete[] numbers;
return0;
}
OutputSuccess
Important Notes
Dynamic memory is stored on the heap, which is different from fixed memory on the stack.
Always free dynamic memory to prevent memory leaks, which can slow down or crash programs.
Summary
Dynamic memory allows programs to use memory flexibly during runtime.
It is useful when the amount of data is not known before running the program.
Remember to free dynamic memory to keep your program healthy.
Practice
(1/5)
1. Why do we need dynamic memory in C++ programs?
easy
A. To write code without any variables
B. To allocate memory during program execution when size is unknown
C. To avoid using any memory at all
D. To make the program run faster by using fixed memory
Solution
Step 1: Understand memory allocation timing
Static memory is fixed before running, but dynamic memory is allocated while the program runs.
Step 2: Recognize the need for unknown sizes
When the amount of data is not known before running, dynamic memory allows flexible allocation.
Final Answer:
To allocate memory during program execution when size is unknown -> Option B
Quick Check:
Dynamic memory = flexible size allocation [OK]
Hint: Dynamic memory = memory decided during program run [OK]
Common Mistakes:
Thinking dynamic memory is for speed only
Confusing dynamic with static memory
Believing dynamic memory avoids variables
2. Which of the following is the correct syntax to allocate an integer dynamically in C++?
easy
A. int *p = new int;
B. int p = new int;
C. int *p = int new;
D. new int = int *p;
Solution
Step 1: Recall dynamic allocation syntax
In C++, dynamic memory for an int is allocated using 'new int' and assigned to an int pointer.
Step 2: Check pointer declaration
The pointer must be declared as 'int *p' to hold the address of the allocated int.
Final Answer:
int *p = new int; -> Option A
Quick Check:
Pointer = new type; correct syntax [OK]
Hint: Use 'int *p = new int;' for dynamic int allocation [OK]
Common Mistakes:
Missing pointer * in declaration
Wrong order of new and type
Assigning new int to non-pointer variable
3. What will be the output of this C++ code snippet?