简体   繁体   中英

Code, static, stack and heap segments implementation

I know the purpose of every segment, but I was wondering who actually implements them in languages such as c or c++?

Are they written by the c/c++ compiler programmers when they write the implementation of the language? Or are these segments implemented at the OS/hardware level(maybe when the OS chooses an address space for a program)?

I imagine a stack frame as a simple c struct which is pushed into the stack for every function call. I imagine the heap as a dynamic array and so on...

This is a little sketchy - HW architectures, memory models etc. all influence the way a C/C++ implementation may feature "segments". Older CPUs had limitations, resulting in a more complicated set of "segments" (iAPX 286 - remember?) So just take this as a crude intro, google for the buzz words,...

Object code contains code resulting from executable statements: bytes resulting from the assembled machine instructions. This will go into a code segement, which will (typically) result in a memory segment that is write protected.

Object code contains data: bytes resulting from assembled data definition statements, with some initialization (or default, zero in case of C/C++), This will go into a data segment, without access restrictions.

A stack is required by the way your CPU works: pushing a return address on the stack, with the most efficient convention of parameter passing by pushing function parameters as well. A "stack frame" is partly "by convention", but typically it consists of the return address and parameters; additional space is reserved for local variables: one set for each instantiation (which is important if the function is recursive).

The heap is just a memory area from which allocations (malloc, new) are being served. It is typically allocated beyond code and data segments. Stacks may be taken out of the heap - here it depends whether you have just a single stack segment or several (think threads).

Additionally, note that there are several object code "formats" or "languages", ie, ways how these segments are being defined in the object code. This depends on what a system's loader can handle: one such format is known as "a.out", another one is "ELF". Compilers must adhere to the format and possibilities.

Are they written by the c/c++ compiler programmers when they write the implementation of the language? Or are these segments implemented at the OS/hardware level(maybe when the OS chooses an address space for a program)?

The segment entry points are primarily managed at the linking stage (and the linker program) of the toolchain.

In that sense, Yes these are implemented by the compiler developers.

You can provide your own linker script though, where you can specify at which concrete memory addresses these segments should occur, and if these memory addresses refer to ROM or RAM.

I imagine a stack frame as a simple c struct which is pushed into the stack for every function call. I imagine the heap as a dynamic array and so on...

It's at all not that simple I'm afraid:

  • The stack frame usually also needs to keep track about the locally instantiated variables, the information necessary for stack unwinding on exceptions, etc.
  • The heap allocated memory needs some basic mechanism to keep track of the allocated memory blocks and their actual size.
  • The static memory initialization and class initialization needs to run the constructors of statically instatiated classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM