Q:
Explain static and dynamic memory allocation with an example each.
Answer
When amount of memory to be allocated is known beforehand i.e. at the the time of compilation, it is known as Static Memory Allocation. Once the memory is allocated statically, it cannot be deallocated during program run. So it leads to wastage of storage space.
Example:
int A[100];
When amount of memory to be allocated is not known beforehand, rather it is determined at the time of program run, it is called Dynamic Memory Allocation. It leads to efficient utilization of storage space.
Example:
cout << " Enter number of elements: ";
cin >> N;
int *A = new int[N]; // dynamic memory allocation
View answer
Workspace
Report Error
Discuss