0% found this document useful (0 votes)
2 views16 pages

Dynamic Memory Allocation v1 12102017

Dynamic memory allocation is the process of allocating memory at runtime using memory management functions. It contrasts with static memory allocation, where global and static variables have fixed memory locations, while local variables are stored in the stack and dynamic memory is allocated in the heap. Key functions for dynamic memory management in C include malloc(), calloc(), realloc(), and free(), which are used for allocating, reallocating, and freeing memory respectively.

Uploaded by

Manmeet Singh
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)
2 views16 pages

Dynamic Memory Allocation v1 12102017

Dynamic memory allocation is the process of allocating memory at runtime using memory management functions. It contrasts with static memory allocation, where global and static variables have fixed memory locations, while local variables are stored in the stack and dynamic memory is allocated in the heap. Key functions for dynamic memory management in C include malloc(), calloc(), realloc(), and free(), which are used for allocating, reallocating, and freeing memory respectively.

Uploaded by

Manmeet Singh
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/ 16

Dynamic Memory

Allocation
Definition
The process of allocating
memory at runtime is known
as dynamic memory
allocation.
Library routines known as
"memory management functions"
are used for allocating and
freeing memory during execution
of a program.
These functions are defined
Difference between static and
dynamic memory allocation
Memory Allocation Process
Global variables, static variables
and program instructions get their
memory in permanent storage area
whereas local variables are stored
in a memory 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.
Allocating block of Memory
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 allocate enough space as
specified, it returns a NULL pointer.
Syntax:

Example using malloc() :


calloc() is another memory allocation
function that is used for allocating
memory at runtime. callocfunction is
normally used for allocating memory
to derived data types such
as arrays and structures. If it fails to
allocate enough space as specified, it
returns a NULL pointer.
Syntax:
Example using calloc() :
Malloc() vs. calloc()
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.
Syntax:

Example using realloc() :


free() function in C
The memory occupied by
malloc() or calloc() functions
must be released by calling free()
function. Otherwise, it will
consume memory until program
exit.
Syntax:

You might also like