×
Widget apcmwh

C Dynamic Memory Allocation


What is Dynamic Memory ?

Sometimes we don't know how much size we need to allocate for a particular task. Maybe it needs 200 bytes or maybe 400 bytes. We just know memory gets allocated dynamically. If task requires 200 bytes then 200 bytes will be allocated. If task reuires 400 bytes then 400 bytes will be allocated. All memory allocation tasks will be performed ot run-time. This is called dynamic memory allocation.

Memory Management Functions

Function Description
malloc() This function reserves a block of memory of given size and returns a pointer of type void.
calloc() calloc function is normally used for allocating memory to derived data types such as arrays and structures.
free() releases previously allocated memory
realloc() modify the size of previously allocated space

Dynamic Memory Structure

Dynamic Memory tasks are performed inside heap . We will learn more about this in Data Structures tutorials. Global variables, static variables and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack. The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing. For now , Memory Structure looks like this ,

Memory Area Storage
Stack --> Local Variables
Heap --> Free memory . Dynamic Memory is allocated here.
Permanent Storage Locations --> Global variables
Permanent Storage Locations --> Program Instructions
Permanent Storage Locations --> static variables

1) malloc()

malloc() function is used for allocating block of memory at runtime. This function reserves a block of memory of given size and returns a pointer of type void. This means that we can assign it to any type of pointer using typecasting. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)malloc(Size_in_bytes);

Example :-

int *x;
x = (int*)malloc(10 * sizeof(int));    //memory space allocated to variable x = 40 bytes
free(x);                               //releases the memory allocated to variable x

2) calloc()

calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating memory to derived data types such as arrays and structures. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)calloc(blocks , size_of_block);

Example :-

struct employee
{
 char *name;
 int salary;
};
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));

3) realloc()

realloc() changes memory size that is already allocated to a variable.

Example :-

int *x;
x=(int*)malloc(50 * sizeof(int));
x=(int*)realloc(x,100); //allocated a new memory to variable x

Difference between malloc() & calloc()

calloc() malloc()
calloc() initializes the allocated memory with 0 value. malloc() initializes the allocated memory with garbage values.
Syntax :

(cast_type *)calloc(blocks , size_of_block);

Syntax :

(cast_type *)malloc(Size_in_bytes);

Number of arguments is 2 Number of argument is 1