new keyword¶
Notes¶
The new
keyword is C++ equivalent of malloc()
from C along with the added benefit of calling the constructor when used with a class. Using new allows you to allocate memory on Heap.
How the new keyword works?¶
- On calling
new
, the new keyword takes the size needed to be loaded in the memory as input. - It searches through the heap to find a continuous block of memory as required.
- After successfully finding the block of memory, it returns the pointer to the memory.
How to load a variable on heap?¶
The var
will store the pointer to the heap memory where the value can be stored. It will be of 4 bytes. (size of int = 4 bytes)
How to load a block of memory?¶
Using []
brackets will allocate 50 block of memory with the size of int. This will be 200 bytes. (50 blocks of size of int = 50 x 4 = 200 bytes)
Important Things to know before using new¶
-
Always clear the memory after the scope of
new
is complete. Since the memory is allocated in heap, it does not auto delete. Clear the memory usingdelete
keyword as follows: -
For a class, though
new
in C++ it equivalent tomalloc
in C, do not use them interchangeably.malloc()
just returns pointer to the memory block when you use it with a class.new
returns the pointer and also initializes the object for the class.