Memory allocation in C is memory management concept in c which is used to allocate the memory in different ways

What is Memory allocation in C?

When we declare a variable of any type the memory for that variable (according to the type of variable) is allocated. Memory allocation in c is a very important feature in C programming which used to save the memory-related issues.
As we declare any variable in c, memory is allocated for that variable.
Different size of memory is stored as we have a different type of data type in c.

Memory allocation means variable get space in memory of the computer (stack or register).
Example: –
int x=10;

Memory allocation in C
Memory allocation in C

Types of Memory allocation in c

In Memory allocation in c, for allocating the memory we have two inbuilt memory allocation functions.
Now, let us see these two functions which are as follows:

  1. Static memory allocation
  2. Dynamic memory allocation

Static memory allocation

Static memory allocation is a memory allocation where variable gets stored statically at compile time.
Memory allocated for the variable has a fixed size in C programming. That is in c different type of data has fixed of size.

Example: –
int a;
=> Memory allocated for int a is 2 byte as storing an integer in c has size 2 byte.
float b;
=> Memory allocated for float is 4 byte.
char c;
=> Memory allocated for the character is 1 byte.
variable size is predefined that’s why memory allocated statically at compile time.

Dynamic memory allocation

Sometimes we require to define data at run time and size of that variable are calculated dynamically at run time when the program is executing.

What is Dynamic memory allocation in C?

Allocating memory at run time is called dynamic memory allocation. That is the memory required by variables for the execution of the program is allocated while executing program.
In c programming, they provided inbuilt functions to allocate memory dynamically.
Whenever we are trying to allocate memory dynamically using the inbuilt function in c, the compiler will allocate the memory for the variable at run time.

In c, we can allocate memory dynamically with the help of 2 functions which are as follows

  1. malloc() function
  2. calloc() function

malloc() function in C :-

malloc is used to allocate memory dynamically. This is the one method using which we can allocate the memory for variables. The malloc function defined in alloc.h header file.

Syntax for malloc: –

pointer-variable = (data type*) malloc (size of(data type));

The malloc function accepts one parameter which is a size of the data type.

Example for malloc: –
int *ptr;
ptr = (int *) malloc (size of (int ));

Program for Memory allocation in C using malloc function

#include <stdio.h>

int main()
{
int *arr;

// malloc() allocate the memory for 5 integers
// containing garbage values
arr = (int *)malloc(5 * sizeof(int)); // 5*4bytes = 5 bytes
printf("\nMemory allocated by malloc :%d",arr);
    return 0;
}

Output: –

Memory allocation in C

Program description:-
1. In the above program, we dynamically created memory using malloc.
2. Using malloc function we defined integer type of data dynamically.
3. int *arr declares integer type of data.
4. arr =(int *)malloc(5 * sizeof(int)) allocates memory for arr variable
5. we print the memory allocated.

Calloc() function in C :-

calloc is contiguous allocation also used for dynamic memory allocation. This is another method using which we can allocate the memory for the specified number of block of a specified type. The calloc function defined in alloc.h header file.

Syntax:- 

pointer – variable = (data type*) calloc (N, size of (data type));

The calloc function accepts two arguments first is the number of element or block you want and second is the size of the type.

Example :-
int *ptr;
ptr = (int *) calloc (size of (int ));

Program for Memory allocation in C using calloc function

#include <stdio.h>

int main()
{
int *arr;

// calloc() allocate the memory for 5 integers and
// set 0 to all of them
arr = (int *)calloc(5, sizeof(int));

printf("\nMemory allocated by calloc :%d",arr);
    return 0;
}

Output:-

Calloc in C

Program description:-

1. In the above program, we dynamically created memory using malloc.
2. Using malloc function we defined integer type of data dynamically.
3. int *arr declares integer type of data.
4. arr =(int *)calloc(5, sizeof(int)) allocates memory for arr variable
5. we print the memory allocated.

Write A Comment