How C++ program stored in
RAM
C/C++ PROGRAM STORED IN RAM MEMORY
The memory layout of program is organized by 4 segments
● Text segment
● Data segment
● Heap segment
● Stack segment
C PROGRAM STORED IN RAM MEMORY
Text segment
● Text segment contains executable instructions of program, it is also
called code segment.
● The text segment is sharable so that only a single copy needs to be in
memory for different executing programs, such as text editors, shells,
and so on.
● Text segment is read-only, to prevent a program from accidentally
modifying its instructions.
Data segment
Two subsections of Data segment
Initialized data
● It contains both static and global data that initialized with non-zero
values.
● This segment can be further classified into the read-only area and read-
write area.
Uninitialized data (BSS segment)
● An uninitialized data segment also called the BSS( ‘Block
Started by Symbol’ ) segment.
● Which contains all global and static variables that
initialized to zero or do not have explicit initialization in
source code.
Heap segment
● The heap segment is an area where dynamically allocated memory by
using (new).
● When we allocate memory through dynamic allocation techniques(in
other words, run-time memory allocation), program acquire space from
OS and process address space grows
(cont)
Heap segment
● We can free dynamically allocated memory space (by using free() or
delete).
● Freed memory goes back to the heap but doesn’t have to be returned
to OS (it doesn’t have to be returned at all), so unordered
malloc/free eventually, cause heap fragmentation.
● When we use dynamic allocation to acquire memory space we must
keep track of allocated memory by using its address.
Stack segment
● The stack segment is an area where local variables stored.
● When we call any function, the stack frame created and when a
function returns, the stack frame destroyed/rewind including all local
variables of that particular function.
● A stack frame contains some data like return address, arguments passed
to it, local variables, and any other information needed by the invoked
function.
Stack Segment
● A stack pointer(SP) which is a special function register of CPU keeps
track of stack by each push & pop operation onto it, by adjusted stack
pointer to next or previous address.
● The direction of the stack & heap growth completely depends on the
compiler, OS and hardware.

How c++ stored in ram

  • 1.
    How C++ programstored in RAM
  • 2.
    C/C++ PROGRAM STOREDIN RAM MEMORY The memory layout of program is organized by 4 segments ● Text segment ● Data segment ● Heap segment ● Stack segment
  • 3.
    C PROGRAM STOREDIN RAM MEMORY
  • 4.
    Text segment ● Textsegment contains executable instructions of program, it is also called code segment. ● The text segment is sharable so that only a single copy needs to be in memory for different executing programs, such as text editors, shells, and so on. ● Text segment is read-only, to prevent a program from accidentally modifying its instructions.
  • 5.
    Data segment Two subsectionsof Data segment Initialized data ● It contains both static and global data that initialized with non-zero values. ● This segment can be further classified into the read-only area and read- write area.
  • 6.
    Uninitialized data (BSSsegment) ● An uninitialized data segment also called the BSS( ‘Block Started by Symbol’ ) segment. ● Which contains all global and static variables that initialized to zero or do not have explicit initialization in source code.
  • 7.
    Heap segment ● Theheap segment is an area where dynamically allocated memory by using (new). ● When we allocate memory through dynamic allocation techniques(in other words, run-time memory allocation), program acquire space from OS and process address space grows (cont)
  • 8.
    Heap segment ● Wecan free dynamically allocated memory space (by using free() or delete). ● Freed memory goes back to the heap but doesn’t have to be returned to OS (it doesn’t have to be returned at all), so unordered malloc/free eventually, cause heap fragmentation. ● When we use dynamic allocation to acquire memory space we must keep track of allocated memory by using its address.
  • 9.
    Stack segment ● Thestack segment is an area where local variables stored. ● When we call any function, the stack frame created and when a function returns, the stack frame destroyed/rewind including all local variables of that particular function. ● A stack frame contains some data like return address, arguments passed to it, local variables, and any other information needed by the invoked function.
  • 10.
    Stack Segment ● Astack pointer(SP) which is a special function register of CPU keeps track of stack by each push & pop operation onto it, by adjusted stack pointer to next or previous address. ● The direction of the stack & heap growth completely depends on the compiler, OS and hardware.