Types of Memory Allocation


In C++, memory allocation can be broadly categorized into three types namely-

1. Static memory allocation: In the static memory allocation, memory is allocates before the execution of the program begins means during the compilation. In simple language, static memory means, how much memory we needed in a program is known to us. In this allocation, pointer is necessary to access the variables. It is implemented using stacks and heaps and variables remain permanently allocated. More memory space is required for static memory allocation.

2. Automatic memory allocation: It is closely related to local variables and uses stack based memory allocation. Whenever a local variable is created, it is pushed into stack. Object is immediately constructed after memory allocation and it is destroyed just before memory deallocation.

3. Dynamic memory allocation: In the dynamic memory allocation, memory is allocated during the execution of program. It is allocated only when program unit is active. Memory bindings are established and destroyed during the execution of program. The execution period of dynamic memory allocation is slower than static memory allocation but it requires less memory space.

Requirement of Dynamic memory allocation in Programming


i. It is best when we don’t know how much memory is require for the execution of the program.

ii. There is no need of dynamically allocated pointers.

iii. Operations such as insertions and deletions can be done very easily just by manipulation of addresses in dynamically created lists, whereas statically allocated insertions and deletions lead to more movements and wastage of memory.

iv. In programming, when we want to use the concept of structures and linked list, dynamic memory allocation is must, etc.

Hence we have tried to implement different functions in our Railway Reservation System using dynamic memory allocation to make the program more efficient on space complexity rather than time complexity.

In this way, we have seen different types of memory allocation. Let’s see how Dynamic memory is allocated in C++ in the next blog.

- Aman Ladkat (K 45)

References - 


Comments

Post a Comment

Popular posts from this blog

Do you have a ticket?

It's moderator's Office

Dynamic Memory Allocation in C++