Are the Heap, Stack, and Data segments the same build program?

As far as I know, the build program is divided into two: 1) code 2) data. Now, when we code, say, in c \ C ++, the code is loaded into memory, and then the CPU starts to execute the code, one instruction at a time, as a build program. My questions: 1. Where is the c-code stored? I mean, when I run the program in Visual Studio, does the code load into one of them - Heap, Stack? 2. Thus, the memory is practically divided into Stack, Heap and Date segments, but when the CPU executes the program as a build program, they are all one build program with the same data area, or they are formed into, say, 2 or 3 assebmly programs that jumps from one to another?

** Let me add this question, perhaps this will clarify my intention: When I run the C program, the code is loaded into memory (machine instructions). So this is one build program. But how is memory sharing? I mean, how do different sections of memory, such as Stack, Data segment, etc., change the build program?

+7
source share
2 answers

Segments or sections are contiguous divisions in object and executable files, like chapters in a book. The stack and bss sections do not exist in the file, but are created at runtime.

The point of the sections is mainly to divide the program into areas that the operating system can protect in different ways. To ensure this, sections should begin at the borders of the page and be contiguous in memory.

Main ("important") sections ...

text or code - the OS will protect this section from writing and, since it is unchanged, it can also share it between several processes or threads that execute the same executable file

data - OS will display this r / w and will not directly 1 share it

bss - this section consists of zero initialized data.

the stack is usually separate from the program, it usually grows down from higher addresses

The last two sections are not in the executable file, because they do not need engineering.

If you ask how they are implemented, the assembler and linker create a table of contents and write sections in binary, like chapters in a book. Then the OS reads them separately and puts them in different sections of the address space.

Specificity and terminology differ between Unix-like systems and Windows, but the principles are the same.


1. Yes, yes, copy-by-copy also allows you to transfer data.
+3
source

From Wikipedia:

The architecture of the PC supports several basic areas of memory for reading and writing, namely: stack, data and code. A heap is another area of ​​address space available to a program from which it can be dynamically allocated or freed up by the operating system in response to system calls such as malloc and free.

I recommend you read the full article

There is also this question about SO: How are the various segments such as heap, stack, text associated with physical memory?

In addition, these articles deserve attention. Especially the last one:

To answer your questions:

1. Where is the c-code stored?

In the code segment.

2. Thus, the memory is practically divided into segments Stack, Heap and Date,

In real mode, yes. In protected mode ... depends. To simplify: program memory is mapped to physical memory. Each program lives in its address space.

I recommend these articles if you want to know more:

3. but when the CPU runs the program as a build program, are they all 1 build program with the same data area or are they formed into, say, 2 or 3 assebmly programs that jump from one to another?

Not. No jumps. The processor registers indicate the next command to be executed. Others point to a stack, etc.

+5
source

All Articles