i am not able to understand the lecture as the lecturer hasn’t made clear where the variables are stored? can u please explain in detail again.I am really confused.
Concept of heap in dynamic memory allocation
@radhika1995 There are two memory areas to be considered- heap and stack.
Heap is a big pool of memory used during Run-Time . Dynamic Memory allocation means that you are allocating memory at run time. So heap memory is considered when you dynamically allocate memory.
In c++, you can dynamically allocate memory using new keyword.
Consider this:
1.
int arr[20];
int a=30;
int *arr;
The value of all these variables will be stored in the stack area of the memory since they are not dynamically allocated.
int *var=new int(30); //to initialize var with a value 30 at run time
int *var= new int; //Without initializing with a particular value
int *arr=new int[n]; //Dynamically allocate an array of size n
A pointer variable is used to store an address .Note that all the pointer variables here ie. *var and *arr are stored in the stack memory area. These variables store the address of the dynamically allocated memory which is in the heap area.
So in int *arr=new int[n] -->> arr is stored in stack memory. new returns the memory location where our array is initialized in heap memory.
Hope this helps