Presentation Description

No description available.

Comments

Posting comment...

Premium member

Presentation Transcript

Dynamic Memory Allocation :

Dynamic Memory Allocation Dept. of IT,
RGUKT,
Basara

Intro :

Intro C language requires the number of elements in an array to be specified at compile time.
Often leads to wastage or memory space or program failure.
Dynamic Memory Allocation
Memory space required can be specified at the time of execution.
C supports allocating and freeing memory dynamically using library routines.

Memory Allocation Process in C :

Memory Allocation Process in C :

Memory Allocation Process in C The program instructions and the global variables are stored in a region known as permanent storage area.
The local variables are stored in another area called stack.
The memory space between these two areas is available for dynamic allocation during execution of the program.
This free region is called the heap.
The size of the heap keeps changing

Memory Layout :

Memory Layout :

Memory Layout

Overview of memory management :

Overview of memory management 11 Stack-allocated memory
When a function is called, memory is allocated for all of its parameters and local variables.
Each active function call has memory on the stack (with the current function call on top)
When a function call terminates,
the memory is deallocated (“freed up”)
Ex: main() calls f(),
f() calls g()
g() recursively calls g() main() f() g() g()

Memory Allocation :

Memory Allocation How is memory allocated?
Global and static variables = program startup

Allocating new heap memory :

Allocating new heap memory void *realloc(void *ptr, size_t new_size);
Given a previously allocated block starting at ptr,
change the block size to new_size,
return pointer to resized block
If block size is increased, contents of old block may be copied to a completely different region
In this case, the pointer returned will be different from the ptr argument, and ptr will no longer point to a valid memory region
If ptr is NULL, realloc is identical to malloc
Note: may need to cast return value of malloc/calloc/realloc:
char *p = (char *) malloc(BUFFER_SIZE);

Memory deallocation :

Memory deallocation How is memory deallocated?
Global and static variables = program finish

Memory deallocation :

Memory deallocation :

Memory deallocation How is memory deallocated?
Global and static variables = program finish
Local variables = function return
Dynamic memory = free()
All memory is deallocated at
program termination

Memory deallocation :

Memory deallocation How is memory deallocated?
Global and static variables = program finish
Local variables = function return
Dynamic memory = free()
All memory is deallocated at
program termination
It is good style to free allocated
memory anyway

Memory deallocation :

Memory deallocation

Deallocating heap memory :

Deallocating heap memory void free(void *pointer);
Given a pointer to previously allocated memory,
put the region back in the heap of unallocated memory
Note: easy to forget to free memory when no longer needed...
especially if you’re used to a language with “garbage collection” like Java
This is the source of the notorious “memory leak” problem
Difficult to trace – the program will run fine for some time, until suddenly there is no more memory!

Checking for successful allocation :

Checking for successful allocation Call to malloc might fail to allocate memory, if there’s not enough available
Easy to forget this check, annoying to have to do it every time malloc is called... Click here to Visualize