Dynamic memory allocation in C

Stack and Heap

Using a combination of RAM and virtual memory, all software programs use their own area of memory called the stack. Every time a function is called in a program, the function’s variables and parameters are pushed onto the program’s memory stack and then pushed off or “popped” when the function has completed or returned

Used for storing variable and parameter contents, memory stacks are dynamic groupings of memory that grow and shrink as each program allocates and deallocates memory

After software programs have terminated, memory is returned for reuse for other software and system programs. Moreover, the operating system is responsible for managing this realm of unallocated memory, known as the heap. Software programs that can leverage memory allocating functions like malloc(), calloc(), and realloc() use the heap

The heap is an area of unused memory managed by the operating system

sizeof in memory allocation

The sizeof operator takes a variable name or data type as an argument and returns the number of bytes required to store the data in memory

#include <stdio.h>
main()
{
 int x;
 float f;
 double d;
 char c;
 typedef struct employee {
 int id;
 char *name;
 float salary;
 } e;
 printf("\nSize of integer: %d bytes\n", sizeof(x));
 printf("Size of float: %d bytes\n", sizeof(f));
 printf("Size of double %d bytes\n", sizeof(d));
 printf("Size of char %d byte\n", sizeof(c));
 printf("Size of employee structure: %d bytes\n", sizeof(e));
}

using malloc function

The malloc() function is part of the standard library and takes a number as an argument. When executed, malloc() attempts to retrieve designated memory segments from the heap and returns a pointer that is the starting point for the memory reserved

#include <stdio.h>
#include <stdlib.h>
main()
{
 char *name;
 name = (char *) malloc(80 * sizeof(char));
 if ( name == NULL )
 printf("\nOut of memory!\n");
 else
 printf("\nMemory allocated.\n");
}

Using free() function

the free() function, which takes a pointer as an argument and frees the memory the pointer refers to

#include <stdio.h>
#include <stdlib.h>
main()
{
 char *name;
 name = (char *) malloc(80*sizeof(char));
 if ( name != NULL ) {
 printf("\nEnter your name: ");
 gets(name);
 printf("\nHi %s\n", name);
 free(name); //free memory resources
 } // end if
}

Dynamic memory allocation using calloc and realloc

A basic implementation of the calloc() function

#include <stdio.h>
#include <stdlib.h>
main()
{
 int *numbers;
 numbers = (int *) calloc(10, sizeof(int));
 if ( numbers == NULL )
 return; // return if calloc is not successful 
}

realloc implementation in dynamic memory allocation

#include<stdio.h>
#include<stdlib.h>
main()
{
 int *number;
 int *newNumber;
 int x;
 number = malloc(sizeof(int) * 5);
 if ( number == NULL ) {
 printf("\nOut of memory!\n");
 return;
 } // end if
 printf("\nOriginal memory:\n");
 for ( x = 0; x < 5; x++ ) {
 number[x] = x * 100;
 printf("number[%d] = %d\n", x, number[x]);
} // end for loop
 newNumber = realloc(number, 10 * sizeof(int)); 
 if ( newNumber == NULL ) {
 printf("\nOut of memory!\n");
 return;
 }
 else
 number = newNumber;
 //intialize new memory only
 for ( x = 5; x < 10; x++ )
 number[x] = x * 100;
 printf("\nExpanded memory:\n");
 for ( x = 0; x < 10; x++ )
 printf("number[%d] = %d\n", x, number[x]);
 //free memory
 free(number);
}

Leave a Comment