Presentation on theme: "Dynamic Memory Allocation in C. What is Memory What is Memory Memory Allocation in C Memory Allocation in C Difference b\w static memory allocation."— Presentation transcript:
What is Memory What is Memory Memory Allocation in C Memory Allocation in C Difference b\w static memory allocation and dynamic memory Difference b\w static memory allocation and dynamic memory Dynamic memory allocation in C Dynamic memory allocation in C Types of Dynamic memory allocation Types of Dynamic memory allocation malloc() function in C malloc() function in C calloc() function in C calloc() function in C Realloc() function in C Realloc() function in C free() function in C free() function in C Difference b\w malloc() and calloc() functions Difference b\w malloc() and calloc() functions
Memory refers to the physical devices used to store programs (sequences of instructions) or data (e.g. program state information) on a temporary or permanent basis for use in a computer or other digital electronic device.
Fixed memory: Executable code Global variables Constant structures that don’t fit inside a machine instruction. Stack memory Local variables for functions, whose size can be determined at call time. Heap memory Structures whose size varies dynamically BACK
The C language supports two kinds of memory allocation through the variables in C programs: Static memory allocation refers to the process of allocating memory at compile-time before the associated program is executed. Dynamic memory allocation or automatic memory allocation where memory is allocated as required at run-time. BACK
The process of allocating memory during program execution is called dynamic memory allocation. Dynamic memory allocation is when an executing program requests that the operating system give it a block of main memory. The program then uses this memory for some purpose. Usually the purpose is to add a node to a data structure. In object oriented languages, dynamic memory allocation is used to get the memory for a new object. Dynamic memory allocation in C BACK
C language offers 4 dynamic memory allocation functions. These are: malloc() calloc() realloc() free()
malloc() function is used to allocate space in memory during the execution of the program. malloc() does not initialize the memory allocated during execution. It carries garbage value. malloc() function returns null pointer if it couldn’t able to allocate requested amount of memory. The syntax is: int * p = (int*) malloc (sizeof (int));
The calloc function is used to allocate storage to a variable while the program is running. This library function is invoked by writing calloc(num,size).This function takes two arguments that specify the number of elements to be reserved, and the size of each element in bytes. The syntax is: int * array = (int *) calloc (10, sizeof (int));
realloc () function modifies the allocated memory size by malloc() and calloc () functions to new size. If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block. The syntax is: void * realloc (void * ptr, size_t size); BACK
free() function frees the allocated memory by malloc(), calloc(),realloc() functions and returns the memory to the system. The syntax is: Free(pointer-name);
Your consent to our cookies if you continue to use this website.