Download Memory layout of c programming and more Slides C programming in PDF only on Docsity!
MEMORY LAYOUT OF C
PROGRAM
Bhagyashree Patra North Orissa University
CONTENTS
- Introduction
- (^) Components of a program
- (^) Memory layout
- (^) A program example
- Conclusion
- (^) References
COMPONENTS OF A PROGRAM
- (^) Functions (caller and Calle)
- (^) Local Variables
- (^) Global Variables
- (^) Static Variables
- Dynamic memory Allocation
MEMORY LAYOUT
Basically, memory layout of C program
contains five segments these are:
Text segment
Initialized data segment (DS)
Uninitialized data segment (BSS)
Stack
Heap
DATA SEGMENT
- This segment contains all the data that is required throughout program execution. In C terms, this includes all extern and static variables. This is split into two parts: Initialized Data Segment:
- (^) It contains all the initialized global and static variables.
- (^) It has read-write permission so the value of the variable of this segment can be changed at run- time.
- (^) This segment can be further classified into an initialized read-only area and initialized read-write area.
DATA SEGMENT CONT.
Unititialized data segment
- (^) It contains all uninitialized global and static
variable.
- (^) All variables in this segment initialized by the
zero(0) and pointer with the null pointer.
- (^) The program loader allocates memory for the
BSS section when it loads the program.
HEAP
- (^) It is used to allocate the memory at run
time.
- (^) The heap area begins at the end of the BSS
segment and goes to larger addresses from
there.
- (^) Heap area managed by the memory
management functions like malloc, calloc ,
free etc which may internally use the brk
and sbrk system is shared by all shared.
- (^) The heap area is shared by all shared
libraries and dynamically loaded modules in
a process.
EXAMPLE PROGRAM… #include <stdio.h> int global_initialized = 21; int global_uninitialized; int func() { int local_inside_func; int *pointer = malloc (sizeof(int)); free(pointer); } int main(int argc, char *argv[] ) { int local_inside_main; func(); return 0; } Text segment a.out Initialized variables global_initialized(21) Uninitialized variable global_uninitialized stack Main() (^) local_inside_main Func() local_inside_func heap in t in t pointer Command line arguments argv[0]^ argv[0] argv[1] argv[1] argv[n]^ argv[n]
REFERENCES
- https://www.geeksforgeeks.org
- (^) https://articleworld.com
- (^) https://tutorialpoint.com
- (^) www.thinkaloudacademy.com
Thank you
Thank youThank you