0% found this document useful (0 votes)
3 views

Dynamic Memory

The document explains dynamic memory allocation in C, highlighting the need to change array sizes at runtime. It details four key functions from the stdlib.h header file: malloc(), calloc(), realloc(), and free(), each serving different purposes for memory management. Additionally, it contrasts static and dynamic memory allocation, emphasizing the flexibility of dynamic allocation during program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Dynamic Memory

The document explains dynamic memory allocation in C, highlighting the need to change array sizes at runtime. It details four key functions from the stdlib.h header file: malloc(), calloc(), realloc(), and free(), each serving different purposes for memory management. Additionally, it contrasts static and dynamic memory allocation, emphasizing the flexibility of dynamic allocation during program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dynamic Memory Allocation

Module-3
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size
of an array.

Here,the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For
example,
If there is a situation where only 5 elements are needed to be entered in this array. In this case, the remaining 4
indices are just wasting memory in this array. So there is a requirement to lessen the length (size) of the array
from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But there is a need to enter
3 more elements in this array. In this case, 3 indices more are required. So the length (size) of the array needs to
be changed from 9 to 12.
Definition
• The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory at
runtime. Dynamic memory allocation in c language is possible by
4 functions of stdlib.h header file.
• malloc()
• calloc()
• realloc()
• free()
Difference between static memory allocation and dynamic
memory allocation.

Static memory allocation Dynamic memory allocation

Memory is allocated at Memory is allocated at run


compile time. time.
Memory can't be increased Memory can be increased
while executing program. while executing program.
Used in array. Used in linked list.
malloc() Allocates single block of requested
memory.

calloc() Allocates multiple block of


requested memory.

Reallocates the memory occupied


realloc() by malloc() or calloc() functions.

free() Frees the dynamically allocated


memory.
malloc() function in C

• The malloc() function allocates single block of requested


memory.
• It doesn't initialize memory at execution time, so it has garbage
value initially.
• It returns NULL if memory is not sufficient.
The syntax of malloc() function is given below:
• ptr=(cast-type*)malloc(byte-size)
calloc() function in C
• The calloc() function allocates multiple block of requested
memory.
• It initially initialize all bytes to zero.
• It returns NULL if memory is not sufficient.
• The syntax of calloc() function is given below:
• ptr=(cast-type*)calloc(number, byte-size)
realloc() function in C

• If memory is not sufficient for malloc() or calloc(), you can


reallocate the memory by realloc() function. In short, it
changes the memory size.
• The syntax of realloc() function:-
ptr=realloc(ptr, new-size)
where ptr is reallocated with new size 'new-size'.
free() function in C

• Free function in C is used to dynamically de-allocate the


memory. The memory allocated using functions malloc() and
calloc() is not de-allocated on their own. Hence the free()
method is used, whenever the dynamic memory allocation
takes place. It helps to reduce wastage of memory by freeing it.
• Syntax of free() in C
free(ptr);
ANY QUESTION?
THANK YOU

You might also like