Agenda
What Is Memory Management in C?
Dynamic Memory Allocation in C
What Is Memory Management in C?
What Is Memory Management in C?
In c programming, the memory is allocated to all the variables of a program automatically during compile
time. Sometimes to reallocate the memory or to resize the size of an array, we must allocate memory
during runtime
Static memory
allocation
Dynamic memory
allocation
There are two ways to allocate memory in C
Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
In dynamic memory allocation, the memory is allocated during run-time and C provides several
functions to allocate memory dynamically.
1. malloc()
2. calloc()
3. realloc()
4. free()
1) malloc() function Syntax:
ptr_variable = *malloc(memory_size);
Example:
ptr=(int*)malloc(10*sizeof(int));
Dynamic Memory Allocation in C
• It first checks the free available memory on the heap and allocates the block of memory size requested.
• If there is not enough memory space to allocate, then the malloc() function returns a zero or null value.
• The pointer variable will hold the address of the first element in the block
2) calloc() function Syntax:
Ptr_variable = *calloc(number_element, memory_size);
Example:
Ptr=(int*)calloc(10, sizeof(int));
Dynamic Memory Allocation in C
calloc() function is like malloc() but the only difference is that the calloc() function will accept two
parameters and allocates multiple block of memory of same size which holds the value zero by default.
3) realloc() function Syntax:
Ptr_variable= *realloc(pointer_variable, memory_size);
Example:
Ptr= (int*)realloc(ptr,5*sizeof(int));
Dynamic Memory Allocation in C
Suppose we need to change the array size of a previously allocated block of memory; in that case, we
use the realloc() function to resize.
4) free() function Syntax:
free(pointer_variable);
Example:
free(ptr);
Dynamic Memory Allocation in C
The free() function is used to de-allocate the memory space occupied by the previously allocated malloc,
calloc or realloc functions.
Dynamic Memory Allocation In C

Dynamic Memory Allocation In C

  • 2.
    Agenda What Is MemoryManagement in C? Dynamic Memory Allocation in C
  • 3.
    What Is MemoryManagement in C?
  • 4.
    What Is MemoryManagement in C? In c programming, the memory is allocated to all the variables of a program automatically during compile time. Sometimes to reallocate the memory or to resize the size of an array, we must allocate memory during runtime Static memory allocation Dynamic memory allocation There are two ways to allocate memory in C
  • 5.
  • 6.
    Dynamic Memory Allocationin C In dynamic memory allocation, the memory is allocated during run-time and C provides several functions to allocate memory dynamically. 1. malloc() 2. calloc() 3. realloc() 4. free()
  • 7.
    1) malloc() functionSyntax: ptr_variable = *malloc(memory_size); Example: ptr=(int*)malloc(10*sizeof(int)); Dynamic Memory Allocation in C • It first checks the free available memory on the heap and allocates the block of memory size requested. • If there is not enough memory space to allocate, then the malloc() function returns a zero or null value. • The pointer variable will hold the address of the first element in the block
  • 8.
    2) calloc() functionSyntax: Ptr_variable = *calloc(number_element, memory_size); Example: Ptr=(int*)calloc(10, sizeof(int)); Dynamic Memory Allocation in C calloc() function is like malloc() but the only difference is that the calloc() function will accept two parameters and allocates multiple block of memory of same size which holds the value zero by default.
  • 9.
    3) realloc() functionSyntax: Ptr_variable= *realloc(pointer_variable, memory_size); Example: Ptr= (int*)realloc(ptr,5*sizeof(int)); Dynamic Memory Allocation in C Suppose we need to change the array size of a previously allocated block of memory; in that case, we use the realloc() function to resize.
  • 10.
    4) free() functionSyntax: free(pointer_variable); Example: free(ptr); Dynamic Memory Allocation in C The free() function is used to de-allocate the memory space occupied by the previously allocated malloc, calloc or realloc functions.