Static and Dynamic Memory Allocation in C
Last Updated :
23 Apr, 2021
Memory is divided into smaller addressable units called bytes. Assume that these are small boxes as bytes. Each byte has its own address as per the below table.For example: 0, 1, 2, 3, 4, 5, 6, etc.

How program uses memory?
The Memory is divided into three sections.
- Heap Memory: It is a part of the main memory. It is unorganized and treated as a resource when you require the use of it if not release. Heap memory can’t be used directly with the help of a pointer.
- Stack Memory: It stores temporary variables created by a function. In stack, variables are declared, stored, and initialized during runtime. It follows the First in last out method that means whatever element is going to store last is going to delete first when it’s not in use.
- Code Section: Whenever the program is executed it will be brought into the main memory. This program will get stored under the code section. Based upon the program it will decide whether to utilize the stack or heap sections.
Below is the image to illustrate how the program uses memory:

Static Memory Allocation
In static memory allocation whenever the program executes it fixes the size that the program is going to take, and it can’t be changed further. So, the exact memory requirements must be known before. Allocation and deallocation of memory will be done by the compiler automatically. When everything is done at compile time (or) before run time, it is called static memory allocation.
Key Features:
- Allocation and deallocation are done by the compiler.
- It uses a data structures stack for static memory allocation.
- Variables get allocated permanently.
- No reusability.
- Execution is faster than dynamic memory allocation.
- Memory is allocated before runtime.
- It is less efficient.
For Example:
C++
#include <iostream>
using namespace std;
void main()
{
int a;
long b;
}
|
Explanation:
- The above piece of code declared 2 variables. Here the assumption is that int takes 2 bytes and long takes 4 bytes of memory. How much memory is taken by variables is again depending upon the compiler.
- These variables will be stored in the stack section. For every function in the program it will take some part of the stack section it is known as the Activation record (or) stack frame and it will be deleted by the compiler when it is not in use.
- Below is the image to illustrate the same:

Below is the C program to illustrate the Static Memory Allocation:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
printf ( "Enter limit of the text: \n" );
scanf ( "%d" , &size);
char str[size];
printf ( "Enter some text: \n" );
scanf ( " " );
gets (str);
printf ( "Inputted text is: %s\n" , str);
return 0;
}
|
Input:

Output:

Advantages:
- Simple usage.
- Allocation and deallocation are done by the compiler.
- Efficient execution time.
- It uses stack data structures.
Disadvantages:
- Memory wastage problem.
- Exact memory requirements must be known.
- Memory can’t be resized once after initialization.
Dynamic Memory Allocation
In Dynamic memory allocation size initialization and allocation are done by the programmer. It is managed and served with pointers that point to the newly allocated memory space in an area which we call the heap. Heap memory is unorganized and it is treated as a resource when you require the use of it if not release it. When everything is done during run time or execution time it is known as Dynamic memory allocation.
Key Features:
- Dynamic allocated at runtime
- We can also reallocate memory size if needed.
- Dynamic Allocation is done at run time.
- No memory wastage
There are some functions available in the stdlib.h header which will help to allocate memory dynamically.
- malloc(): The simplest function that allocates memory at runtime is called malloc(). There is a need to specify the number of bytes of memory that are required to be allocated as the argument returns the address of the first byte of memory that is allocated because you get an address returned, a pointer is the only place to put it.
Syntax:
int *p = (int*)malloc(No of values*size(int));
The argument to malloc() above clearly indicates that sufficient bytes for accommodating the number of values of type int should be made available. Also notice the cast (int*), which converts the address returned by the function to the type pointer to int. malloc() function returns a pointer with the value NULL.
- calloc(): The calloc() function offers a couple of advantages over malloc(). It allocates memory as a number of elements of a given size. It initializes the memory that is allocated so that all bytes are zero. calloc() function requires two argument values:
- The number of data items for which space is required.
- Size of each data item.
It is very similar to using malloc() but the big plus is that you know the memory area will be initialized to zero.
Syntax:
int *p = (int*)calloc(Number of data items, sizeof(int));
- realloc(): The realloc() function enables you to reuse or extend the memory that you previously allocated using malloc() or calloc(). A pointer containing an address that was previously returned by a call to malloc(), calloc(). The size in bytes of the new memory that needs to be allocated. It allocates the memory specified by the second argument and transfers the contents of the previously allocated memory referenced by the pointer passed as the first argument to the newly allocated memory.
Syntax:
int *np = (type cast) realloc (previous pointer type, new number of elements * sizeof(int));
- . free(): When memory is allocated dynamically it should always be released when it is no longer required. Memory allocated on the heap will be automatically released when the program ends but is always better to explicitly release the memory when done with it, even if it’s just before exiting from the program. A memory leak occurs memory is allocated dynamically and reference to it is not retained, due to which unable to release the memory.
Syntax:
free(pointer);
For Example:
C
#include <iostream>
using namespace std;
void main()
{
int * p;
P = ( int *) malloc (5 * sizeof ( int ));
}
|
Examples:
- In the above piece of code, a pointer p is declared. Assume that pointer p will take 2 bytes of memory and again it depends upon the compiler.
- This pointer will store in the stack section and will point to the array address of the first index which is allocated in the heap. Heap memory cannot be used directly but with the help of the pointer, it can be accessed.

- When the program is not in use, the memory should be deallocated. Otherwise, it will cause a memory leak.

- After deallocating the memory allocated in the heap. Below is the image to illustrate the main memory after deallocation.

Below is the C program to illustrate the Dynamic Memory Allocation:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size, resize;
char * str = NULL;
printf ( "Enter limit of the "
"text: \n" );
scanf ( "%d" , &size);
str = ( char *) malloc (size * sizeof ( char ));
if (str != NULL) {
printf ( "Enter some text: \n" );
scanf ( " " );
gets (str);
printf ( "Inputted text by allocating"
"memory using malloc() is: "
"%s\n" ,
str);
}
free (str);
str = ( char *) calloc (50, sizeof ( char ));
if (str != NULL) {
printf ( "Enter some text: \n" );
scanf ( " " );
gets (str);
printf ( "Inputted text by allocating "
"memory using calloc() is: "
"%s\n" ,
str);
}
printf ( "Enter the new size: \n" );
scanf ( "%d" , &resize);
str = ( char *) realloc (str, resize * sizeof ( char ));
printf ( "Memory is successfully "
"reallocated by using "
"realloc() \n" );
if (str != NULL) {
printf ( "Enter some text: \n" );
scanf ( " " );
gets (str);
printf ( "Inputted text by reallocating"
" memory using realloc()is: "
"%s\n" ,
str);
}
free (str);
str = NULL;
return 0;
}
|
Input:

Output:

Advantages:
- Dynamic Allocation is done at run time.
- We can allocate (create) additional storage whenever we need them.
- Memory can be deallocated (free/delete) dynamic space whenever we are done with them.
- Thus, one can always have exactly the amount of space required – no more, no less.
- Memory size can be reallocated if needed.
Disadvantages:
- As the memory is allocated during runtime, it requires more time.
- Memory needs to be freed by the user when done. This is important as it is more likely to turn into bugs that are difficult to find.
Similar Reads
C Programming Language Tutorial
C is a general-purpose, procedural, and middle-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It is also known as the "mother of all programming languages" as it influenced many modern programming languages like C++, Java, Python, and Go. Why learn C?The C la
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory. To resolve this
10 min read
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Example: [GFGTABS] C++ int number; [/GFGTABS]The above statement declares a variable with name number that can store integer values. C is a static
6 min read
C Arrays
An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc. Array DeclarationIn C,
9 min read
C Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
10 min read
C Language Introduction
C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableP
6 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Basics of File Handling in C
File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. Need of File Ha
13 min read
Operators in C
In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples. What is an Operator in C?A C operator can be defined as the symbol that helps us
13 min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if bot
7 min read