C – Dynamic memory allocation

Prev     Next

Dynamic memory allocation in C:

The process of allocating memory during program execution is called dynamic memory allocation.

Dynamic memory allocation functions in C:

C language offers 4 dynamic memory allocation functions. They are,

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
Function
Syntax
malloc () malloc (number *sizeof(int));
calloc () calloc (number, sizeof(int));
realloc () realloc (pointer_name, number * sizeof(int));
free () free (pointer_name);

1. malloc() function in C:

  • 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.

Example program for malloc() function in C:

Output:

Dynamically allocated memory content : fresh2refresh.com

2. calloc() function in C:

  • calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t.

Example program for calloc() function in C:

Output:

Dynamically allocated memory content : fresh2refresh.com

3. realloc() function in C:

  • 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.

 4. free() function in C:

  • free () function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system.

Example program for realloc() and free() functions in C:

Output:

Dynamically allocated memory content : fresh2refresh.com
Resized memory : space is extended upto 100 characters

Difference between static memory allocation and dynamic memory allocation in C:

Static memory allocation Dynamic memory allocation
In static memory allocation, memory is allocated while writing the C program. Actually, user requested memory will be allocated at compile time. In dynamic memory allocation, memory is allocated while executing the program. That means at run time.
Memory size can’t be modified while execution. 
Example: array
Memory size can be modified while execution. 
Example: Linked list

Difference between malloc() and calloc() functions in C:

malloc() calloc()
It allocates only single block of requested memory It allocates multiple blocks of requested memory
int *ptr;ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory only allocated in one block. 
Total = 80 bytes
int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory will be created and each contains 20*4 bytes of memory. 
Total = 1600 bytes
malloc () doesn’t initializes the allocated memory. It contains garbage values calloc () initializes the allocated memory to zero
type cast must be done since this function returns void pointer int *ptr;ptr = (int*)malloc(sizeof(int)*20 ); Same as malloc () function int *ptr;ptr = (int*)calloc( 20, 20 * sizeof(int) );

Prev     Next





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!