← Back to list

Dynamic Memory Allocation in C

What is Dynamic Memory Allocation?

Dr. Vipin Kumar · 2021-02-23 09:45 · 0 claps · 1.6 min read
#dynamic-memory-allocation #c-programming #malloc #calloc
Open on Medium ↗
Wiki topics: 💻 · Programming

Dynamic Memory Allocation in C

  1. What is Dynamic Memory Allocation?

Runtime memory allocation is called dynamic memory allocation.

  1. Why Dynamic Memory?

A. Efficient use of memory by saving waste memory.

B. To overcome Array size limitation.

C. To create new data structure using linked list, graph, stack and queue.

*3. *How can be performed dynamic memory allocation in C.

A. using malloc() function

B. using realloc() function

C. using calloc() function

  1. How can be free used memory?

A. using free() function

5. Use of malloc() function

A. it take one parameter as size of memory allocation.

B. it return void pointer (generic: it may int, float, long, char and etc. )*

C. void malloc(size_t size);*

For Example:

*int p=(int ) malloc(5sizeof(int)); //20 bytes

//p pointer have base or starting address of that allocated memory

Let’s take program to understand it:

#include<stdio.h>

#include<stdlib.h>

*int main(int argc, char const argv[])

{

int num;

printf(“Enter size for data”);

scanf(“%d”,&num);

*int data=(int )malloc(numsizeof(int));

printf(“Enter 5 element in array”);

for(int i=0;i<num;i++)

{

scanf(“%d”,data+i);

}

printf(“Value after inserting is=”);

for(int j=0;j<num;j++)

{

*printf(“%d “,(data+j));

}

free(data);

return 0;

}

  1. Use of realloc() function

A. it use to reallocate memory to older pointer if required without deleting existing values.

B. it take two parameter, one as old pointer and second as re-size

C. it return void pointer.*

D. void realloc(void oldpointer,size_t re-size);

For Example:

*int newp =(int )realloc(p,7sizeof(int));

Let’s take program to understand it:

#include<stdio.h>

#include<stdlib.h>

*int main(int argc, char const argv[])

{

int num,num1;

printf(“Enter size for data”);

scanf(“%d”,&num);

*int data=(int )malloc(numsizeof(int));

printf(“Enter 5 element in array”);

for(int i=0;i<num;i++)

{

scanf(“%d”,data+i);

}

printf(“Value after inserting is=”);

for(int j=0;j<num;j++)

{

*printf(“%d “,(data+j));

}

printf(“\nEnter re-size for data again”);

scanf(“%d”,&num1);

data =(int )realloc(data,num1sizeof(int));

printf(“\nEnter remaining values”);

for(int i=num;i<num1;i++)

{

scanf(“%d”,data+i);

}

printf(“\nValue after re-size are=”);

for(int j=0;j<num1;j++)

{

*printf(“%d “,(data+j));

}

free(data);

return 0;

}

  1. Use of calloc() function

A. it is use to allocate block of memory at runtime.

B. All blocks are initialized with zero.

C. it takes two parameters first number of block and second size of one block.

D. void calloc(number of block, size of one block);*

For Example:

int p =(int )calloc(5,sizeof(int));

Let’s take program to understand it:

#include<stdio.h>

#include<stdlib.h>

*int main(int argc, char const argv[])

{

int size;

int size2;

printf(“Enter size of block”);

scanf(“%d”,&size);

int data=(int )calloc(size,sizeof(int));

printf(“\nDefault value of all block are”);

for(int j=0;j<size;j++)

{

*printf(“%d “,(data+j));

}

printf(“\nEnter %d value”,size);

for(int i=0;i<size;i++)

{

scanf(“%d”,data+i);

}

printf(“\nValue of calloc array is=”);

for(int j=0;j<size;j++)

{

*printf(“%d “,(data+j));

}

free(data);

return 0;

}


메타데이터
post_id
ea8f1872f71a
slug
dynamic-memory-allocation-in-c-ea8f1872f71a
url
https://medium.com/@geniusvipin/dynamic-memory-allocation-in-c-ea8f1872f71a
canonical_url
https://medium.com/@geniusvipin/dynamic-memory-allocation-in-c-ea8f1872f71a
author_url
https://medium.com/@geniusvipin
status
ok
fetched_at
2026-07-28 12:19:09