Download presentation
Presentation is loading. Please wait.
Published byRoy Rhode Modified over 9 years ago
1
Dynamic Memory Allocation in C
2
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
3
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.
4
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
5
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
7
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
8
C language offers 4 dynamic memory allocation functions. These are: malloc() calloc() realloc() free()
9
BACK
10
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));
11
#include main() { char *mem; clrscr(); mem = malloc( 20 * sizeof(char)); if( mem== NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem,"fresh2refresh.com"); } printf("Dynamically allocated memory content :%s\n", mem); free(mem); getch(); }
12
BACK
13
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));
14
#include int main() { char *mem_allocation; mem_allocation = calloc( 20, sizeof(char) ); if( mem_allocation== NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"fresh2refresh.com"); } printf("Dynamically allocated memory content :%s\n", mem_allocation ); free(mem_allocation); }
15
BACK
16
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
17
free() function frees the allocated memory by malloc(), calloc(),realloc() functions and returns the memory to the system. The syntax is: Free(pointer-name);
18
#include int main() { char *mem_allocation; mem_allocation = malloc( 20 * sizeof(char) ); if( mem_allocation == NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"fresh2refresh.com"); }
19
printf("Dynamically allocated memory content : %s\n", mem_allocation ); mem_allocation=realloc(mem_allocation,100*sizeof(char)); if( mem_allocation == NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"space is extended upto " \ "100 characters"); } printf("Resized memory : %s\n", mem_allocation ); free(mem_allocation); }
20
BACK
22
BACK TO INDEX
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.