Dynamic memory allocation in C

Preview
You must first complete Introduction to pointer in C | pointer operations in C before viewing this Lesson

Dynamic memory allocation in C

C language offers various functions to allocate and de-allocate memory to variables at run time. Various functions for performing these functions are;

  • malloc()
  • calloc()
  • free()




1. malloc() function

malloc() is a predefined function provided by C language which is used to dynamically allocate memory to variable at run time. malloc() allocates a block of size bytes from the memory heap.

By default, compiler of C language automatically allocates memory to a variable but programmer can also allocate memory to a variable as needed and in the exact amounts needed.

For that purposes, programmer can use the malloc() function of C language. But before using this function, the variable whose memory is to be allocated must be declared as pointer variable and then must be allocated memory using malloc() function.

The header file required for malloc() is stdlib.h.

Syntax:
malloc(size);

Here, size specifies the numbers of bytes to be allocated to the variable. This size must be equal to the size of the data type of the pointer variable.

Example
int *a;
a=malloc(2);

In the above example, a is a pointer variable of int type which has been allocated memory of 2 bytes using malloc() function .

It must be noted that if programmer allocates memory to a variable using malloc(), he must de-allocate the memory using free() function otherwise memory allocated to variable using malloc() will not be released and system may crash.

Program to demonstrate the use of malloc().
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *s; 

s = malloc(2); //2 bytes allocated to s
*s=15;

 

printf(“*s=%d”,*s);
free(s); //Memory allocated to variable s is released herereturn (0);
}

Output
*s=15




2. calloc() functiou

calloc() is a predefined function provided by C language to dynamically allocate memory to variable at run time. calloc() allocates a block of size bytes from the memory heap.

By default, compiler of C language automatically allocates memory to a variable but programmer can also allocate memory to a variable as needed and in the exact amounts needed.

For that purposes, programmer can use the calloc() function of C language. But before using this function, the variable whose memory is to be allocated must be declared as pointer variable and then must be allocated memory using calloc() function.

The header file required for calloc() is stdlib.h.

Syntax:
calloc(items, size);

Here, items specifies the number of memory blocks to be allocated to the variable.
size specifies the numbers of bytes to be allocated to the variable. This size must be equal to the size of the data type of the pointer variable.
calloc() allocated the memory equal to items*size.

Example
int *a;
a=calloc(1, sizeof(int));

In the above example, a is a pointer variable of int type which has been allocated memory of 2 bytes [sizeof(int)] using calloc() function. Number of memory blocks to be allocated is 1.
If a programmer allocates memory to a variable using calloc(), he must de-allocate the memory using free() function, otherwise memory allocated to variable using calloc() will not be released and system may crash.

Program to demonstrate the use of calloc().
#include<stdio.h>
#include<stdlib.h>
int main()

{

int *s;

s= calloc(1,sizeof(int)); /*1 block of size 2 bytes is allocated*/

*s=15;
printf(“*s=%d”,*s);

free(s);

return(0);

}

Output
*s=15




free() function

free() function is dynamic memory de-allocation function. It performs opposite to malloc() and calloc() functions.

It is used to de-allocate memory assigned to a variable using dynamic memory allocation functions malloc() and calloc().

Example:
free(a);

The above command will dealloacte memory assigned to pointer variable a.

Memory Leak

What happens if some memory is allocated, but never deallocated? A program which forgets to deallocate a block is said to have a “memory leak” which may or may not be a serious problem. The result will be that the heap memory fills up due to continous allocation requests, but no deallocation requests to return blocks for re-use.

Memory leaks are more of a problem for a program which runs for an indeterminate amount of time. In that case, the memory leaks can gradually fill the heap until allocation requests cannot be satisfied, and the program stops working or crashes.

Many commercial programs have memory leaks, so that when run for long enough, or with large data-sets, they fill their heaps and crash. Often the error detection and avoidance code for the heap-full error condition is not well tested, precisely because the case is rarely encountered with short runs of the program — that’s why filling the heap often results in a real crash instead of a polite error message.

Most compilers have a “heap debugging” utility which adds debugging code to a program to track every allocation and deallocation. When an allocation has no matching deallocation, that’s a leak, and the heap debugger can help you find them.


Best Books of C





Lesson tags: calloc() function in c, dynamic memory allocation in c, dynamic memory deallocation in c, free() function in c, malloc() function in c, memory leak in c, program of dynamic memory allocation in c, program of dynamic memory deallocation in c
Back to: C Programming Language