MEMORY LAYOUT OF THE PROGRAM
Example program:
#include<stdio.h>
int gloabl_initalized=1; /* stored in initialized variables RW data segment*/
int gloabl un_initialized; /* Uninitialized variable stored in bss data segment*/
static int initialized=1; /* stored in initialized variables RW data segment */
staic int un_initialized; /* Uninitialized variable stored in bss data segment*/ *
const char * name="hello world"; /* string literal stored in initialized read only DS*/
/* character pointer variable string in initialized read-write area. */
int main()
{
static int a; /* Uninitialized static variable stored in bss data segment*/
static in a =100 /* Initialized static variable stored in DS*/
}
Source:
http://www.geeksforgeeks.org/memory-layout-of-c-program/
- BSS - block started by symbol
- Un Initialized data segment variables initialized to zero by default by kernel
- Stack frame used to point to top of the stack and when every time new function called then stack frame pushed in to the STACK segment of the memory and stack pointer adjusted and this new stack frame(function which is pushed newly into stack segment) stores the return address of the caller, and machine registers of the caller and automatic local variables.
- function calls follows LIFO technique
- heap memory can be allocated using memalloc,calloc, free using system calls: brk/sbrk/mmap
- Local variables and function frame - stack
- Global and static variables if uninitialized - .bss block start by symbol
- Global and static variables if initialized - data segment
- Environment variables and arguments - on top of the stack
- Dynamic data allocation - heap
- Const - ROM
- Volatile - no storage
- Register - cpu register
- Const volatile - in the same place as const storage
1. Check the following simple C program
#include <stdio.h> int main( void ) { return 0; } |
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout [narendra@CentOS]$ size memory-layout text data bss dec hex filename 960 248 8 1216 4c0 memory-layout
2. Let us add one global variable in program, now check the size of bss (highlighted in red color).
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/ int main( void ) { return 0; } |
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text data bss dec hex filename
960 248 12 1220 4c4 memory-layout
3. Let us add one static variable which is also stored in bss.
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/ int main( void ) { static int i; /* Uninitialized static variable stored in bss */ return 0; } |
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text data bss dec hex filename
960 248 16 1224 4c8 memory-layout
4. Let us initialize the static variable which will then be stored in Data Segment (DS)
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/ int main( void ) { static int i = 100; /* Initialized static variable stored in DS*/ return 0; } |
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text data bss dec hex filename
960 252 12 1224 4c8 memory-layout
5. Let us initialize the global variable which will then be stored in Data Segment (DS)
#include <stdio.h> int global = 10; /* initialized global variable stored in DS*/ int main( void ) { static int i = 100; /* Initialized static variable stored in DS*/ return 0; } |
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text data bss dec hex filename
960 256 8 1224 4c8 memory-layout
Ref:http://www.geeksforgeeks.org/memory-layout-of-c-program/
Types of object file:
These object files are as follows:
- Relocatable object file: These are static library files. Static linkers such as the Unix
ld
program take collection of relocatable object files and command line arguments as input and generate a fully linked executable object file as output that can be loaded into memory and run. Relocatable object files contain binary code and data in a form that can be combined with other relocatable object files at compile time to create an executable object file. - Executable object file: These are executable files contain binary code and data in a form that can be copied directly into memory and executed.
- Shared object file: These special type of relocatable object files are loaded into memory and linked dynamically, at either load time or run time.
Ref:
http://cs-fundamentals.com/c-programming/memory-layout-of-c-program-code-data-segments.php
http://blmrgnn.blogspot.in/2014/06/memory-layout-of-c-program-code-data.html
Refer this too:
==========
https://netospgm.blogspot.com/2019/06/how-to-put-break-points-for-static.html
To know how function call works in the memory
please refer the below link:http://abrickshort.wordpress.com/2005/04/10/how-function-calls-work/
https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/08/lec.html
in the stack the order how the new function call works
pushing(calling funcion argument)
pushing (old frame pointer. i.e. caller's frame pointer)
pushing (return address)
pushing(local variables)
Reference:
http://stackoverflow.com/questions/10430047/where-are-constant-volatile-variables-stored-in-c
No comments:
Post a Comment