Dynamic Memory Allocation


Prev Tutorial Next Tutorial

Dynamic Memory Allocation

  • 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

	
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

Advertisements




Facebook Likes

Advertisements