Dynamic Memory Allocation in C

While doing programming we may go over a circumstance when the quantity of information things continues

changing during the program execution. For instance, we have made a program that stores the subtleties for

representatives in an organization. The rundown of workers continues changing as new representatives

join or old workers leave the organization. At the point when this rundown builds we need more memory to store

the data of new workers. Essentially when some worker leaves we have to free the memory with the goal that it tends to be

used to store some other data. Such a condition can be overseen successfully with the assistance of the idea of dynamic memory assignment.

Dynamic Memory Allocation in C

The procedure of distribution of memory at the run time is known as unique memory portion.

In C it is finished utilizing four memory the executive’s functions that are given beneath. In this instructional exercise,

we will examine them individually in detail.

malloc()

calloc()

free()

realloc()

Note: The header record utilized for these four functions is stdlib.h.

In beneath picture you can see that there is a free space between nearby

factors and worldwide factors district. This free space is known as stack and it is utilized for dynamic memory portion.

Dynamic Memory Allocation in C

malloc() in C

A square of memory can be designated utilizing malloc() function. Each dispensed byte contains trash esteem. Check its syntax underneath.

Syntax

pointer_name = (caste_type *) malloc(size_in_bytes);

Example

p = (int *) malloc (100 * sizeof (int) );

Above articulation will distribute a memory space of multiple times the size of int information type.

It implies on the off chance that whole number takes 2 bytes, at that point 200 bytes will be assigned.

Here sizeof() is an administrator that will give the size of int information type. The malloc() function will restore the location of the

first byte of memory allotted and this location will be put away in pointer p.

Let’s take one more example.

p = (burn *) malloc(10);

This will assign 10 bytes of memory which can store just character type esteems.

malloc() distributes square of adjoining bytes. In the event that it can’t locate the predefined size

space in the pile then it will return NULL. It is great to check the pointer variable to guarantee that the memory designation was effective.

calloc() in C

It is utilized for designating various squares of memory of the same size. Every byte is naturally set to zero.

This function is typically used to designate memory for inferred information types like exhibits and structures.

Syntax

pointer_name = (cast_type *) calloc(n, size_in-bytes);

example

. . . . .
struct student
{
 char name[20];
 int roll_no;
}*p;
 
p = (struct student*) calloc(5, sizeof(struct student) );
. . . . .

This will apportion 5 squares of memory of the same size that can store five records of structure under study.

On the off chance that calloc() can’t locate the predetermined memory space then it will return NULL.

free() in C

At the point when we dispense memory powerfully then it’s

our duty to free the memory when it is never again being used, with the goal that it very well may be utilized to

store some other data. In C it very well may be finished utilizing the free() function.

Syntax

free(pointer_name);

example

free(p);

Here p is the pointer variable that contains the location of the first byte of the memory square assigned by either malloc() or calloc() function.

realloc() in C

To change the size of the memory allotted powerfully we use realloc() function.

We may confront a condition that the recently designated memory isn’t adequate to store the information and

more space is required. Or on the other hand, we have assigned a memory space bigger than our need.

In these cases, we can without much of a stretch adjust the size of distributed memory utilizing realloc().

Syntax

pointer_name = realloc(pointer_name, new_size);

example

p = malloc(10);

p = realloc(p,20);

realloc() assigns new memory square and duplicate the information present in past designated

square to new square. The recently assigned memory square is naturally liberated. It likewise return NULL

if can’t locate the predefined memory space in load.

Let’s make one program to comprehend the utilization of all these memories the board functions in C.

#include<stdio.h>
#include<stdlib.h>
 
void main()
{
 int *p,i;
  
 p=(int*)malloc(3*sizeof(int));
 
 //checking the memory allocation was successful
 if(p==NULL)
 {
  printf("nInsufficient memory");
  exit(0);
 }
 
 printf("Enter three numbers:");
 for(i=0;i<3;++i)
  scanf("%d",p+i);
  
 for(i=0;i<3;++i)
  printf("%d ",*(p+i));
  
 //altering the memory
 p=realloc(p,5*sizeof(int));
 
 //checking the memory allocation was successful
 if(p==NULL)
 {
  printf("nInsufficient memory");
  exit(0);
 }
 
 printf("nnEnter two more numbers:");
 scanf("%d%d",p+3,p+4);
 
 for(i=0;i<5;++i)
  printf("%d ",*(p+i));
 
 //free the memory
 free(p);
}

Output:

enter three numbers:3 5 6 
356
Inter two more numbers:8 9 
 3 5 6 8 9 
Press any key to continue . . . _ 

Clarification

In the above program, I am the first dispensing memory space to store three whole number qualities utilizing malloc() function. You can obviously observe that I am checking the pointer p before utilizing to guarantee that the memory distribution was fruitful.

After that, I have taken three whole number qualities and afterwards, put away them in memory and showed them.

Presently I am adjusting the size of memory that I have recently assigned. I am changing the memory size utilizing realloc() function with the goal that I can store two more whole number qualities.

Subsequent to perusing two additional qualities I am showing every one of the qualities once more. You can find in the yield, the three qualities that I put away before stay unaltered.

Finally, I have liberated the memory utilizing the free() function.

So this is about powerful memory assignment in C. In the event that you discover anything absent or inaccurate in above instructional exercise, at that point please educate me. Don’t hesitate to inquire as to whether you have any questions with respect to above instructional exercise.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen − thirteen =

Recent Content