ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT

Chapter 14: C Language Dynamic Memory Allocation

prodevelopertutorial April 10, 2020

In this chapter we shall study about following topics

  1. Introduction
  2. Malloc
  3. Calloc
  4. Re-alloc
  5. Free

 

1. Introduction

 

In C program there are 2 ways to allocate memory.

  1. Compile time memory allocation: We use this type of allocation when we know the size of elements that we are storing before execution of a program.
  2. Runtime memory allocation: We use this type when we don’t know the size of elements. Hence we allocate the memory at run time.

 

When the memory is allocated at compile time, the memory is allocated in stack segment.

When the memory is allocated at run time, the memory is allocated in heap segment.

 

C provides 4 functions to manage memory at the runtime:

  1. malloc() : Used to create dynamic memory at runtime.
  2. calloc() : Another version of malloc.
  3. realloc() : Used to reallocate the memory.
  4. free() : Used to free the allocated memory

 

2. malloc():

Malloc stands for memory allocation.

It will allocate the required memory and give the pointer to that memory location.

If it returns NULL, then memory is not allocated.

The initial values will be garbage value in that memory.

The pointer returned to that memory location is of type void. Hence there is a need to type cast that pointer.

 

Syntax:

ptr = (type*) malloc(size-to-be-allocated);

 

Example:

ptr = (int*) malloc (50 * sizeof(int));

Here the compiler will give 100 bytes or 200 bytes based on the size of int is 2 or 4 bytes.

 

3. calloc():

Calloc stands for contiguous allocation.

It will allocate the required memory and give the pointer to that memory location.

If it returns NULL, then memory is not allocated.

The initial value will be set to zero,

The pointer returned to that memory location is of type void. Hence there is a need to type cast that pointer.

 

Syntax:

ptr = (type*) calloc(n, size-of-the-element);

example:

ptr = (int*) calloc (50 * sizeof(int));

Here the compiler will give 100 bytes or 200 bytes based on the size of int is 2 or 4 bytes.

 

4. realloc():

When the memory is allocated by using the above functions is not enough, then we use realloc to re-allocate the memory.

If the new memory is less than the reserved size, then the same memory block with extra space is returned.

 

Of the requested memory is greater than the reserved size, then new memory address will be returned.

 

It will return memory address if allocation is successful, NULL if memory is not available.

 

Syntax:

<data_type*>realloc(<pointer>, size);

 

5. free()

The memory allocated by using alloc functions, we need to free those memories. This prevents wastage of memory and memory leak.

 

Syntax:

free(<ptr>);

Program to show memory allocation functions:

#include<stdio.h>
//#include<alloc.h>

int main()
{
	char *cptr_1 = NULL;
	char *cptr_2 = NULL;

	// allocate memory using malloc
	cptr_1 = (char *) malloc(100 * sizeof(char));

	printf("The address of allocated memory by malloc is %u\n",cptr_1 );

	// allocate memory using calloc
	cptr_2 = (char *) calloc(100, sizeof(char));
	printf("The address of allocated memory by calloc is %u\n",cptr_2 );

	// re-allocate memory using realloc with in the reserved size,
	// hence it will return the same address adding extra space
	cptr_2 = (char *) realloc(cptr_2, 100);
	printf("The address of re-allocated memory is %u\n",cptr_2 );

	// re-allocate memory using realloc exceeding reserved size,
	// hence it will return new address adding extra space
	cptr_2 = (char *) realloc(cptr_2, 1000);
	printf("The address of re-allocated memory is %u\n",cptr_2 );

	//free the pointer
	free(cptr_1);
	free(cptr_2);

	// assign null to those pointers
	cptr_2 = NULL;
	cptr_1 = NULL;
	return 0;
}

Output:

The address of allocated memory by malloc is 205530880
The address of allocated memory by calloc is 205530992
The address of re-allocated memory is 205530992
The address of re-allocated memory is 205531136

 

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Daily we discuss about competitive programming questions, join us at:   Telegram Channel

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2021 ProDeveloperTutorial.com