new and delete Operators in C++ For Dynamic Memory (original) (raw)
Last Updated : 15 May, 2025
In C++, when a variable is declared, the compiler automatically reserves memory for it based on its data type. This memory is allocated in the program's stack memory at **compilation of the program. Once allocated, it cannot be deleted or changed in size. However, C++ offers manual low-level memory management through dynamic memory allocation.
What is Dynamic Memory Allocation?
**Dynamic memory allocation is the process of **allocating memory at the **runtime of a program. It allows programmers to reserve some memory during the program's execution, use it as required and then free it to use it for some other purpose. This memory is allocated in the **Heap memory of the program instead of the stack memory. It is very useful in cases like:
- When you are not sure about the size of the array you need.
- Implementing data structures such as linked list, trees, etc.
- In complex programs that require efficient memory management.
**Dynamic memory allocation in C++ and deallocation is achieved by using two specialized operators: **new and **delete.
new Operator
The **new operator requests for the allocation of the block of memory of the given size of type on the **Free Store (name for the part of heap memory available for new operator). If sufficient memory is available, a new operator initializes the memory to the default value according to its type and returns the address to this newly allocated memory.
Syntax
C++ `
new data_type;
`
In the above statement, a memory block that can store a single value of given **data_type is reserved in the heap and the address is returned. This address should be stored in the pointer variable of the same type.
**Example:
C++ `
int *nptr = new int;
`
We allocated the memory for a single integer using new and stored its address in the integer pointer **nptr. We can also initialize the allocated memory by providing an initial value:
C++ `
int *nptr = new int(10);
`
**Example:
C++ `
#include <bits/stdc++.h> using namespace std;
int main() {
// Declared a pointer to store
// the address of the allocated memory
int *nptr;
// Allocate and initialize memory
nptr = new int(6);
// Print the value
cout << *nptr << endl;
// Print the address of memory
// block
cout << nptr;
return 0;
}
`
Allocate Block of Memory (Array)
A **new operator is also used to dynamically allocate a block (an **array) of memory of given data type as shown below:
C++ `
new data_type[n];
`
This statement dynamically allocates memory for **n elements of given **data_type. Arrays can also be initialized during allocation.
**Example:
C++ `
#include <bits/stdc++.h> using namespace std;
int main() {
// Declared a pointer to store
// the address of the allocated memory
int *nptr;
// Allocate and initialize array of
// integer with 5 elements
nptr = new int[5]{1, 2, 3, 4, 5};
// Print array
for (int i = 0; i < 5; i++)
cout << nptr[i] << " ";
return 0;
}
`
**What if enough memory is not available during runtime?
If enough memory is not available in the heap to allocate, the new request indicates failure by throwing an exception of type std::bad_alloc, unless "nothrow" is used with the new operator, in which case it returns a **nullptr pointer. Therefore, it may be a good idea to check for the pointer variable produced by the new before using its program.
C++ `
int *p = new (nothrow) int; if (!p) { cout << "Memory allocation failed\n"; }
`
delete Operator
In C++, **delete operator is used to release dynamically allocated memory. It deallocates memory that was previously allocated with new.
Syntax
C++ `
delete ptr;
`
where, **ptr is the pointer to the dynamically allocated memory.
To free the dynamically allocated array pointed by pointer variable, use the following form of delete:
C++ `
delete[] arr;
`
**Example:
C++ `
#include using namespace std;
int main() { int *ptr = NULL;
// Request memory for integer variable
// using new operator
ptr = new int(10);
if (!ptr) {
cout << "allocation of memory failed";
exit(0);
}
cout << "Value of *p: " << *ptr << endl;
// Free the value once it is used
delete ptr;
// Allocate an array
ptr = new int[3];
ptr[2] = 11;
ptr[1] = 22;
ptr[0] = 33;
cout << "Array: ";
for (int i = 0; i < 3; i++)
cout << ptr[i] << " ";
// Deallocate when done
delete[] ptr;
return 0;
}
`
Output
Value of *p: 10 Array: 33 22 11
Errors Associated with Dynamic Memory
As powerful as dynamic memory allocation is it is also prone to one of the worst errors in C++. Major ones are:
**Memory Leaks
Memory leak is a situation where the memory allocated for a particular task remains allocated even after it is no longer needed. Moreover, if the address to the memory is lost, then it will remain allocated till the program runs.
**Solution: Use smart pointers whenever possible. They automatically deallocate when goes out of scope.
**Dangling Pointers
Dangling pointers are created when the memory pointed by the pointer is accessed after it is deallocated, leading to undefined behaviour (crashes, garbage data, etc.).
**Solution: Initialize pointers with nullptr and assign nullptr again when deallocated.
**Double Deletion
When delete is called on the same memory twice, leading to crash or corrupted program.
**Solution: assign nullptrto the memory pointerwhen deallocated.
**Mixing new/delete with malloc()/free()
C++ supports the C style dynamic memory allocation using malloc(), calloc(), free(), etc. But these functions are not compatible. It means that we cannot allocate memory using new and delete it using free(). Same for malloc() and delete.
Placement new
**Placement new is a variant of **new operator. Normal **new operator both allocates memory and constructs an object in that memory. On the other hand, the placement new separates these actions. It allows the programmer to pass a pre-allocated memory block and construct an object in that specific memory.