Sunday 1 July 2012

C Programming - Dynamic Memory allocation


C Programming - Dynamic Memory allocation:


Embedded and other low-memory footprint applications need to be easy on the amount of memory they use when executing.  In such scenarios, statically sized data types and data structures just are not going to solve your problem.  The best way to achieve this is by allocation of memory for variables at runtime under your watchful eye. This way your program is not using more memory than it has to at any given time. However, it is important to note that the amount of memory that can be allocated by a call to any of these functions is different on every O.S. .

Dynamic Memory Allocation:

malloc, calloc, or realloc are the three functions used to manipulate memory.  These commonly used functions are available through the stdlib library so you must include this library in order to use them.

#include stdlib.h 
After including the stdlib library you can use the malloc, calloc, or realloc functions to manipulate chunks of memory for your variables.

Dynamic Memory Allocation Process:

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.

Allocating a Block of Memory

Use the malloc function to allocate a block of memory for a variable. If there is not enough memory available, malloc will return NULL.
The prototype for malloc is:


void *malloc(size_t size);



Do not worry about the size of your variable, there is a nice and convenient function that will find it for you, called sizeof. Most calls to malloc will look like the following example:

ptr = (struct mystruct*)malloc(sizeof(struct mystruct));


This way you can get memory for your structure variable without having to know exacly how much to allocate for all its members as well.

Allocating Multiple Blocks of Memory:

You can also ask for multiple blocks of memory with the calloc function:


void *calloc(size_t num, size_t size);

If you want to allocate a block for a 10 char array, you can do this:



  1. char *ptr;
  2. ptr = (char *)calloc(10, sizeof(char));
The above code will give you a chunk of memory the size of 10 chars, and the ptr variable would be pointing to the beginning of the memory chunk. If the call fails, ptr would be NULL.

Releasing the Used Space:

All calls to the memory allocating functions discussed here, need to have the memory explicitly freed when no longer in use to prevent memory leaks. Just remember that for every call to an *alloc function you must have a corresponding call to free.
The function call to explicitly free the memory is very simple and is written as shown here below:
free(ptr);
Just pass this function the pointer to the variable you want to free and you are done.
To Alter the Size of Allocated Memory
Lets get to that third memory allocation function, realloc.

void *realloc(void *ptr, size_t size);
Pass this function the pointer to the memory you want to resize and the new size you want to resize the allocated memory for the variable you want to resize.




No comments:

Post a Comment