Featured Post

ARM- ADC and LCD Interface

In real world all natural quantities like temperature, pressure, light intensity etc. are analog. If we have to deal with such quantities us...

Friday 26 July 2019

Dynamic Memory Allocation.

Dynamic Memory Allocation 

Most often we face situations in programming where the data is dynamic in nature. i.e. the number of data items keep changing during the execution of the program. For example, consider a program for processing the list of customers of a corporation. The list grows when names are added and shrinks when names are deleted. When list grows we need to allocate more memory space to the list to accommodate additional data items. Such situations can be handled more easily and effectively by using what is known as dynamic data structures in conjunction with dynamic memory management techniques. Dynamic data structure provides flexibility in adding, deleting or rearranging data items at run time. Dynamic memory management (i.e. DMA) technique permits to allocate additional memory space or to release unwanted space at run time, thus optimizing the usage of storage space. 

 Introduction 

The exact size of an array is unknown until the compile-time i.e. time when a compiler compiles code written in a programming language into an executable form. The size of the array you have declared initially can be sometimes insufficient and sometimes more than required. What? The process of allocating memory during program execution is called dynamic memory allocation. It also allows a program to obtain more memory space, while running or to release space when no space is required. 

Following example will help you understand use of dynamic memory allocation in any program.


WAP to find out the smallest and largest element sorted in an array of n integers.




code contributed by anmol sinha

Memory Allocation Process

Global variables, static variables and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack. The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing when program is executed due to creation and death of variables. Therefore it is possible to encounter memory “overflow” during dynamic allocation process. In such cases, the memory allocation process will return a NULL pointer.




Watch this Youtube video to better understand the topic.




More examples :

No comments:

Post a Comment