Dynamic Memory Allocation

Definition :

  • At runtime, we can able to decide storage capacity means we can decide the memory size whether we have to increment or decrement.

There are two types memory allocations :

  1. Static memory allocation
  2. Dynamic memory allocation

Functions used to allocate memory dynamically :

  1. Malloc
  2. Calloc
  3. realloc

Syntax :

void *calloc(size_t nmemb, size_t size)

void *malloc(size_t size)

void free(void *ptr)

void *realloc(void *ptr,size_t size)
  • This memory allocation will take place at heap section.
  • #include<stdlib.h> must be included during work with DMA.
  • Whenever we allocating Dynamic Memory, we must deal with a pointer so that we can catch it. the where in static memory we can directly access it.
  • Dynamic Memory is a contiguous memory.
  • Minimum 4bytes is required to access dynamic memory.
  • If we lost the address of dynamically allocated memory that memory becomes lick.
  • After freeing a memory, if a pointer is still holding that memory address than that pointer is called the dangling pointer.
  • So, We get data may or may not be.After freeing a memory make the pointer NULL.

Realloc:

  • realloc allocates previously allocated memory.
  • syntax :
p = realloc(previously allocated memory, the memory size you want to allocate);
  • malloc allocates some extra memory for pointing purpose.
realloc (p,0) = free(p);  //both is equal to each other
  • If realloc fails to reallocates the memory than previously allocated memory will be untouched.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: