Dynamic Memory Allocation

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Dynamic memory allocation:

An array is a collection of fixed number of values of a single type. That is, you need
to declare the size of an array before you can use it.
Sometimes, the size of array you declared may be insufficient. To solve this issue,
you can allocate memory manually during run-time. This is known as dynamic
memory allocation in C programming.
There are 4 library functions defined under <stdlib.h> makes dynamic memory
allocation in C programming. They are malloc(), calloc(), realloc() and free().

malloc()
The name "malloc" stands for memory allocation.
The malloc() function reserves a block of memory of the specified number of bytes.
And, it returns a pointer of type void which can be casted into pointer of any form.

Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)

Example:

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

Considering the size of int is 4 bytes, this statement allocates 400 bytes of memory.
And, the pointer ptr holds the address of the first byte in the allocated memory.
However, if the space is insufficient, allocation fails and returns a NULL pointer.

calloc()
The name "calloc" stands for contiguous allocation.
The malloc() function allocates a single block of memory.
Whereas, calloc() allocates multiple blocks of memory and initializes them to zero.

Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);

Example:

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

This statement allocates contiguous space in memory for 25 elements each with the
size of float.

C free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.
Syntax of free()
free(ptr);

This statement frees the space allocated in the memory pointed by ptr.

realloc()
If the dynamically allocated memory is insufficient or more than required, you can
change the size of previously allocated memory using realloc() function

Syntax of realloc()
ptr = realloc(ptr, x);

Here, ptr is reallocated with new size x.

Static allocation:

Advantages:

1. Static allocation is done at compile time when you know the size of the array.

2.The memory size allocated to “data” is static. But it is possible to change content of
a static structure without increasing the memory space allocated to it.

3.Global variables are declared “ahead of time,” such as fixed array.

4.Lifetime of Static allocation is the entire runtime of program.

5. It has efficient execution time

Disadvantages:

1. In case more static data space is declared than needed, there is waste of space.

2. In case less static space is declared than needed, then it becomes impossible to
expand this fixed size during run time.

Dynamic Allocation:
Advantages:

·Dynamic Allocation is done at run time.

·Data structures can grow and shrink to fit changing data requirements.

·We can allocate (create) additional storage whenever we need them.

·We can de-allocate (free/delete) dynamic space whenever we are done with them.  

·Thus we can always have exactly the amount of space required - no more, no less.

Disadvantages:

·As the memory is allocated during runtime, it requires more time.

·Memory needs to be freed by the user when done. This is important as it is more
likely to turn into bugs that are difficult to find.

You might also like