2. Dynamic memory allocation refers to performing
manual memory management for dynamic
memory allocation in the C programming language
via a group of functions in the C standard library.
C Dynamic Memory Allocation
01010010 01000001
01001011 01001001
01000010
3. Why Dynamic Memory Allocation ?
In many situations the
programmer requires
greater flexibility in
managing the lifetime of
allocated memory.
01010010 01000001
01001011 01001001
01000010
4. As you know, an array is a collection of a fixed number of
values. Once the size of an array is declared, you cannot change
it.
Sometimes the size of the 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.
01010010 01000001
01001011 01001001
01000010
5. Overview of functions
Function Description
malloc() allocates the specified number of bytes
realloc() increase or decrease the size of the specific block of
memory , moving it if necessary
calloc() allocates the number of bytes and initialize them to zero
free() releases the specified block of memory back to system
The C dynamic memory allocation functions are defined in stdlib.h
6. The name "malloc" stands for memory allocation.
C malloc()
01010010 01000001
01001011 01001001
01000010
Syntax of malloc()
Example
The malloc() function reserves a block of memory of the
specified number of bytes. And, it returns a pointer of void
which can be casted into pointers of any form.
7. The name "calloc" stands for contiguous allocation.
C calloc()
01010010 01000001
01001011 01001001
01000010
Syntax of calloc()
Example
The malloc() function allocates memory and leaves the memory
uninitialized. Whereas, the calloc() function allocates memory
and initializes all bits to zero.
8. C free()
01010010 01000001
01001011 01001001
01000010
Syntax of free()
Dynamically allocated memory created with either
calloc() or malloc() dosen’t get freed on their own. You
must explicitly use free() to release the space.
This statement frees the space allocated in the memory
pointed by ptr .
9. C realoc()
If the dynamically allocated memory is insufficient or
more than required, you can change the size of previously
allocated memory using the realloc() function.
Syntax of realloc()
Here ptr is reallocated with a new size x .
01010010 01000001
01001011 01001001
01000010
10. Usage examples
Creating an array of ten integers with automatic scope is straightforward in
C:
However, the size of the array is fixed at compile time. If one wishes to
allocate a similar array dynamically, the following code can be used:
01010010 01000001
01001011 01001001
01000010
11. Because malloc() might not be able to service the request , it might be return
a null pointer and it is good programming practice to check this:
When the program no longer needs the dynamic array, it must eventually call
free to return the memory it occupies to the free store:
12. With realloc we can resize the amount of memory a pointer points
to. For example, if we have a pointer acting as an array of size n
and we want to change it to an array of size m, we can use realloc.
Note that realloc must be assumed to have changed the base address of
the block . 01010010 01000001
01001011 01001001
01000010
13. Example 1: malloc()
and free()
Here, we have dynamically allocated
the memory for n number of int .
01010010 01000001
01001011 01001001
01000010