Memory Management in C

Saidsadaoy
5 min readAug 7, 2023

--

Memory blocks are allocated for program entities during execution phase.

Memory allocation is of three types in C program:

Static Memory Allocation

Static allocation happens when you declare a static or global variable. The space is allocated once, when your program is started and remain reserved till the end of the program. These variables are stored in a data segment. A data segment is a fixed size area in the memory set aside for these variables.

Automatic Memory Allocation

Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered and is freed when that compound statement is exited.These variables are stored in a stack. A stack is an area of memory that starts out small and grows automatically up to some predefined limit. The stack is kept in the region of memory known as the stack segment.

Dynamic Memory Allocation

Memory is allocated dynamically to the entities of the program. Programmer has freedom to allocate and free the memory for the program entities. Such a memory allocation is called dynamic memory allocation.This memory area is known as a heap. A heap is another area that starts out small and grows, but it grows only when there is an explicit call to some memory allocation functions like malloc, calloc or realloc, free.

-The malloc function:-

Syntax

void *malloc( size_t size );

How to use?

  • The malloc function allocates the memory space for an object whose size is specified by the parameter size.
  • The malloc function returns a void pointer to the first byte of the allocated space, if successful.
  • malloc function does not initialize allocated space to any value.
  • If the memory space cannot be allocated, a null pointer is returned.
  • malloc is faster than calloc as it does not initialize allocated space.

Example

-The calloc function:-

Syntax

void *calloc(size_t n, size_t size);

How to use?

  • The calloc function allocates the memory space for an array of n objects, each of whose size is specified by the parameter size.
  • Unlike malloc, calloc initializes every byte of the block allocated with the value 0.
  • The calloc function returns a void pointer to the first element of the allocated elements, If successful.
  • If the memory space cannot be allocated, a null pointer is returned.
  • calloc takes little longer than malloc because of the extra step of initializing the allocated memory by zero.

Example

#include <stdio.h>  
#include <stdlib.h>
int main ()
{
// This pointer will hold the base address of th block created
int* ptr;
int n, i;

// Get the number of elements for the array
n = 5;
printf("Enter number of the elements:%d\n", n);

// Dynamically allocate memory using calloc or not
ptr = (int*)calloc (n, sizeof(int));

if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else {
//Memory has been successfully allocated
printf("Memory has been successfully allocated.\n");

//Get the elements of the array
printf("the elements of the array are: ");
for(i = 0; i < n; i++) {
printf("%d, ", ptr[i]);
}
}

return 0;
}

-The realloc function:-

Syntax

void *realloc(void *ptr, size_t size);

How to use?

  • When dynamically allocated memory appears too large or too small then realloc function can be used to resize allocated memory without losing old data
  • realloc function reallocates the given area of memory. It must be previously allocated by malloc, calloc or realloc.
  • The realloc function accepts two arguments, the first argument ptr is a pointer to the first byte of memory that was previously allocated and second argument is the new size of the block in bytes, which may be smaller or larger than the original size.
  • If the memory for the new object cannot be allocated, the old object is not deallocated and its value remains unchanged.
  • The realloc function returns a void pointer to the new object, or a null pointer if the new object cannot be allocated.
  • If ptr given to the realloc function is a null pointer, the realloc function behaves like the malloc function.
  • If the second argument passed to the realloc function is zero, it behaves like a free function.

Example:-

#include <stdio.h>  
#include <stdlib.h>

int main ()
{
// This pointer will hold the base address of th block created
int *ptr;
int n, i;

// Get the number of elements for the array
n = 5;
printf("Enter number of the elements:%d\n", n);

// Dynamically allocate memory using calloc or not
ptr = (int*)calloc(n, sizeof(int));

if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else {
//Memory has been successfully allocated
printf("Memory has been successfully allocated using calloc.\n");

//Get the elements of the array
for(i = 0; i < n; ++i) {
ptr[i] = i + 1;
}

// Print the elements of the array
printf("The elements of the array\n");

//Get the new size for the array
n = 10;
printf("\n\nEnter the new size of the array: %d\n", n);

// Dynamically re-allocate memory using realloc();
ptr = realloc(ptr, n * sizeof(int));

//Memory has been successfully allocated
printf("Memory has been successfully allocated using realloc.\n");

//Get the elements of the array
for(i = 5; i < n; ++i) {
ptr[i] = i + 1;
}

//print the elements of the array
printf("the elements of the array are: ");

for(i = 0; i < n; ++i) {
printf("%d", ptr[i]);
}
free(ptr);

}

return 0;
}

-The free function:-

Syntax

void free(void *ptr);

How to use?

  • Dynamically allocated memory does not get freed on their own. You must explicitly use free function to deallocate the memory.
  • free function deallocates the memory previously allocated by a call to malloc, calloc or realloc.
  • The free function returns no value.
  • To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

Example:-

#include <stdio.h>  
#include <stdlib.h>

int main ()
{
// This pointer will hold the base address of th block created
int *ptr, *ptr1;
int n, i;

// Get the number of elements for the array
n = 5;
printf("Enter number of the elements:%d\n", n);

// Dynamically allocate memory using malloc or not
ptr = (int*)Malloc(n * sizeof(int));

// Dynamically allocate memory using malloc or not
ptr1 = (int*)calloc(n, sizeof(int));

if (ptr == NULL || ptr1 == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else {
//Memory has been successfully allocated
printf("Memory has been successfully allocated using malloc.\n");
//Free the memory
free(ptr);
printf("malloc memory successfully freed\n");

//Memory has been successfully allocated
printf("Memory has been successfully allocated using calloc.\n");
//Free the memory
free(ptr1);
printf("calloc memory successfully freed.\n");
}

return 0;
}

There is permanent storage, Heap, and Stack.

The heap, like the stack, has a limit on how much it can grow.

--

--