Are data segments,heap,stack etc also present in secondary memory with compiled code then later mapped by OS to RAM when I open it?
Under most operating systems the binary executable and object files stored on disk contain three segments:
- the .text segment (the code)
- the .data segment stores the initial value for any global and static variables that need to be initialized to specific values other than zero.
- the .bss segment stores the global and static variables that don't need to be initialized, or are initialized to 0. Typically only the size of the .bss segment is stored since the loader can easily create a segment in RAM of this size and fill it with zeros without wasting the time and disk space to store a bunch of redundant information in the executable stored on disk.
The wikipedia page https://en.wikipedia.org/wiki/Data_segment#Data gives some examples.
The stack and heap are typically not stored in the binary executable. They are set up by the loader in RAM when the program is started.