position: fixed; top: auto !important; margin-left: 112px;

Dynamic Memory Allocation in C

Consider this array declaration,

int students[50]

In this array declaration, memory of 50*4 = 200 bytes will be reserved. Now, suppose you need memory only for 30 students or maybe memory for 70 students. If you notice, there is some restriction for allocation of memory. You have to think always how much bytes an array will require.

What if memory will be allocated at runtime?

This is done using standard library functions malloc() and calloc(). These functions are called as Dynamic Memory Allocation functions in C.

C Memory Management Functions

Functions Description
malloc() This function reserves a block of memory of given size and returns a pointer of type void.
calloc() calloc function is normally used for allocating memory to derived data types such as arrays and structures.
free() releases previously allocated memory
realloc() modify the size of previously allocated space

1) malloc()

malloc function is used to allocate block of memory of given size and returns a pointer of type void. Since malloc() returns a void pointer you can typecast it to integer pointer,float pointer according to the need of program. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)malloc(Size_in_bytes);

Example :-

#include "alloc.h"
main()
{
  int n, *p;
  printf("Enter the number of students");
  scanf("%d", &n);
  
  p = (int *)malloc(n*4);
  if(p == NULL)
  {
    printf("Memory is not allocated due to some problem");
  }
}

As you see, here memory allocation depends on the user input. If user inputs 20 for Enter the number of students then malloc function will allocate 20*4 = 80 bytes of memory. And (int *) is typecast since malloc returns void pointer.

2) calloc()

calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating memory to derived data types such as arrays and structures. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)calloc(blocks , size_of_block);

Example :-

struct employee
{
 char *name;
 int salary;
};
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));

3) realloc()

realloc() changes memory size that is already allocated to a variable.

Example :-

int *x;
x=(int*)malloc(50 * sizeof(int));
x=(int*)realloc(x,100); //allocated a new memory to variable x

Difference between malloc and calloc

Let's have a look at the difference between calloc and malloc functions..

calloc() malloc()
calloc() initializes the allocated memory with 0 value. malloc() initializes the allocated memory with garbage values.
Syntax :

(cast_type *)calloc(blocks , size_of_block);

Syntax :

(cast_type *)malloc(Size_in_bytes);

Number of arguments is 2 Number of argument is 1

Dynamic Memory Structure

Dynamic Memory tasks are performed inside heap. We will learn more about this in Data Structures tutorials. Global variables, static variables and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack. The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing. For now , Memory Structure looks like this ,

Memory Area Storage
Stack --> Local Variables
Heap --> Free memory . Dynamic Memory is allocated here.
Permanent Storage Locations --> Global variables
Permanent Storage Locations --> Program Instructions
Permanent Storage Locations --> static variables


Solve the Quiz of Article

1) When we dynamically allocate memory is there any way to free memory during run time?
Yes
No

2) Can we increase the size of statically allocated array?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.