Dynamic memory allocation in C


Hi friends today we are going to discuss about dynamic memory allocation, this plays a very important role in embedded development. Specially when your product is based on data entry type of application.

Dynamic allocation is a unique feature to C language. Dynamic memory allocation allows a program to obtain more memory space, while running or to release space when no space is required. It is the practice of assigning memory locations during execution of the program by explicit request of the programmer. It enables us to create data types and structures of any size and length to suit our programs need within the program.

dynamic_mem.fw

Just think of arrays, we need to determine the size of array first hand itself, but by using dynamic allocation we eliminate the need to determine the size of array at declaration time. Dynamically allocated memory area is secured. Variables are usually allocated to the location of the stack; but for dynamic allocation heap memory area is used. When a program executes, the operating system gives it a stack and a heap to work with. The stack is where global variables, static variables, and functions and their locally defined variables reside. The heap is a free section for the program to use for allocating memory at runtime.

Although, C language inherently does not has any technique to allocated memory dynamically, there are 4 library functions under “stdlib.h” for dynamic memory allocation.

malloc()

The name malloc stands for “memory allocation”. The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.

Syntax of malloc()

melloc.fw

Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.

ptr=(int*)malloc(50*sizeof(int));

This statement will allocate either 100 or 200 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

calloc()

The name calloc stands for “contiguous allocation”. The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.

Syntax of calloc()

calloc.fw

This statement will allocate contiguous space in memory for an array of n elements. For example:

ptr=(float*)calloc(20,sizeof(float));

This statement allocates contiguous space in memory for an array of 20 elements each of size of float, i.e, 4 bytes.

realloc()

If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc().

Syntax of realloc()

realloc.fw

free()

Dynamically allocated memory with either calloc() or malloc() does not get return on its own. The programmer must use free() explicitly to release space.

syntax of free()

free.fw

This statement cause the space in memory pointer by ptr to be deallocated.

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (3)
  1. Piyush says - Posted: April 6, 2013

    But I think this is not possible with microcontrollers right, because they dont have an OS to provide memory from heap. It might be possible with RTOS and linux based platform like ARM or raspberry pi etc. ?

  2. admin says - Posted: April 17, 2013

    Yes Zahiirp you are right, it is quite possible…thanks for sharing link

Leave your comment

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