Open In App

Static and Dynamic Memory Allocation in C

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. 



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:

For Example:




// C++ program to illustrate the
// concept of memory allocation
#include <iostream>
using namespace std;
  
// Driver Code
void main()
{
    int a; // 2 bytes
    long b; // 4 bytes
}

Explanation:

Below is the C program to illustrate the Static Memory Allocation:




// C program to implement
// static memory allocation
#include <stdio.h>
#include <stdlib.h>
  
// Driver code
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:

Disadvantages:

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:

There are some functions available in the stdlib.h header which will help to allocate memory dynamically.

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.

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));

Syntax:

int *np = (type cast) realloc (previous pointer type, new number of elements * sizeof(int));

Syntax:

free(pointer);

For Example:




// C program to illustrate the concept
// of memory allocation
#include <iostream>
using namespace std;
  
// Driver Code
void main()
{
    int* p; // 2 bytes
    P = (int*)malloc(5 * sizeof(int));
}

Examples: 

Below is the C program to illustrate the Dynamic Memory Allocation:




// C program to illustrate the above
// concepts of memory allocation
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
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 is not NULL
    if (str != NULL) {
        printf("Enter some text: \n");
        scanf(" ");
        gets(str);
        printf("Inputted text by allocating"
               "memory using malloc() is: "
               "%s\n",
               str);
    }
  
    // Free the memory
    free(str);
    str = (char*)calloc(50, sizeof(char));
  
    // If str is not NULL
    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 is not NULL
    if (str != NULL) {
        printf("Enter some text: \n");
        scanf(" ");
        gets(str);
        printf("Inputted text by reallocating"
               " memory using realloc()is: "
               "%s\n",
               str);
    }
  
    // Free the memory
    free(str);
    str = NULL;
  
    return 0;
}

Input:

Output:

Advantages:

Disadvantages:


Article Tags :