Below is a basic memory architecture used by any C++ program:
new
KeywordHere we will learn how to allocate heap memory to a variable or class object using the new
keyword.
Syntax:
datatype pointername = new datatype
For example:
int *new_op = new int;
// allocating block of memory
int *new_op = new int[10];
If enough memory is not available in the heap it is indicated by throwing an exception of type std::bad_alloc
and a pointer is returned.
delete
KeywordOnce heap memory is allocated to a variable or class object using the new
keyword, we can deallocate that memory space using the delete
keyword.
Syntax:
delete pointer variable
For example:
delete new_op;
The object's extent or the object's lifetime is the time for which the object remains in the memory during the program execution. Heap Memory allocation is slower than a stack. In heap there is no particular order in which you can allocate memory as in stack.
Memory leak happens due to the mismanagement of memory allocations and deallocations. It mostly happens in case of dynamic memory allocation. There is no automatic garbage collection in C++ as in Java, so programmer is responsible for deallocating the memory used by pointers.
Misuse of an elevator in a building in real life is an example of memory leak. Suppose you stay in an apartment building which has 19 floors. You wanted to go to 10th floor so you pressed the button to call the lift. The status of elevator is displaying as basement for 20 minutes. Then you realize that something is wrong, and upon investigating you find out that kids were playing in basement and they had blocked the lift door.
Similarly once a pointer is done with its operations it should free the memory used by it. So that other variables can use the memory and memory can be managed effectively.
By using the delete
keyword we can delete the memory allocated:
For example:
*ex= new Example();
delete ex;
But in the above example dangling pointer issue can happen. Wait! what is a dangling pointer?
A pointer pointing to a memory location of already deleted object is known as a dangling pointer.
Dangling pointers arise due to object destruction, when an object reference is deleted or deallocated, without modifying the value of the pointer, so the pointer will keep on pointing to the same memory location. This problem can be avoided by initializing the pointer to NULL
.
For example:
*ex = new Example();
Delete ex;
// assigning the pointer to NULL
ex = NULL;
Smart Pointer is used to manage the lifetimes of dynamically allocated objects. They ensure proper destruction of dynamically allocated objects. Smart pointers are defined in the memory header file.
Smart pointers are built-in pointers, we don't have to worry about deleting them, they are automatically deleted.
Here is an example of a smart pointer:
S_ptr *ptr = new S_ptr();
ptr->action();
delete ptr;