Dynamic Memory Allocation in C

Google Advertisements

Prev Tutorial Next Tutorial

Dynamic Memory Allocation in C

  • It is a process of allocating or de-allocating the memory at run time it is called as dynamically memory allocation.
  • When we are working with array or string static memory allocation will be take place that is compile time memory management.
  • When we ate allocating the memory at compile we cannot extend the memory at run time, if it is not sufficient.
  • By using compile time memory management we cannot utilize the memory properly
  • In implementation when we need to utilize the memory more efficiently then go for dynamic memory allocation.
  • By using dynamic memory allocation whenever we want which type we want or how much we type that time and size and that we much create dynamically.

Dynamic memory allocation related all predefined functions are declared in following header files.

  • <alloc.h>
  • <malloc.h>
  • <mem.h>
  • <stdlib.h>

Dynamic memory allocation related functions

Malloc()

By using malloc() we can create the memory dynamically at initial stage. Malloc() required one argument of type size type that is data type size malloc() will creates the memory in bytes format. Malloc() through created memory initial value is garbage.

Syntax:

	
Void*malloc(size type);

Note: Dynamic memory allocation related function can be applied for any data type that's why dynamic memory allocation related functions return void*.

When we are working with dynamic memory allocation type specification will be available at the time of execution that's why we required to use type casting process.

Syntax of Dynamic Memory Allocation in C

	
int *ptr;
ptr=(int*)malloc(sizeof (int));	//2 byte

long double*ldptr;
ldptr=(long double*)malloc(sizeof(long double))	// 2 byte

char*cptr;
cptr=(char*)malloc(sizeof(char));	//1 byte

int*arr;
arr=(int*)malloc(sizeof int()*10);	//20 byte

cahr*str;
str=(char*)malloc(sizeof(char)*50);	//50 byte

calloc()

  • By using calloc() we can create the memory dynamically at initial stage.
  • calloc() required 2 arguments of type count, size-type.
  • Count will provide number of elements; size-type is data type size.
  • calloc() will creates the memory in blocks format.
  • Initial value of the memory is zero.

Syntax

	
int*arr;
arr=(int*)calloc(10, sizeof(int));	// 20 byte
cahr*str;
str=(char*)calloc(50, siceof(char));	// 50 byte

realloc()

  • By using realloc() we can create the memory dynamically at middle stage.
  • Generally by using realloc() we can reallocation the memory.
  • Realloc() required 2 arguments of type void*, size_type.
  • Void* will indicates previous block base address, size-type is data type size.
  • Realloc() will creates the memory in bytes format and initial value is garbage.

Syntax

	
void*realloc(void*, size-type);
int *arr;
arr=(int*)calloc(5, sizeof(int));
.....
........
....
arr=(int*)realloc(arr,sizeof(int)*10);

free()

  • When we are working with dynamic memory allocation memory will created in heap area of data segment.
  • When we are working with dynamic memory allocation related memory it is a permanent memory if we are not de-allocated that's why when we are working with dynamic memory allocation related program, then always recommended to deleted the memory at the end of the program.
  • By using free(0 dynamic allocation memory can be de-allocated.
  • free() requires one arguments of type void*.

Syntax

	
void free(voie*);
int *arr;
arr=(int*)calloc(10,sizeof(int));
...
......
free(arr);
  • By using malloc(), calloc(), realloc() we can create maximum of 64kb data only.
  • In implementation when we need to create more than 64kb data then go for formalloc(), forcalloc() and forrealloc().
  • By using free() we can de-allocate 64kb data only, if we need to de-allocate more than 64kb data then go for

Syntax

	
forfree().
formalloc()
voidfor*formalloc(size-type);

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders. Difference between path and classpath in Java

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs