简体   繁体   中英

Which Data structure in each segment of memory layout of C?

I know the memory layout of a C program is divided into text, heap, stack, data and bss segments. I think (not sure) that this memory layout alone is the reason behind maintaining the scope and lifetime of variables of different storage classes.

For example, auto variables are stored in the stack. Each time a function call happens, a new stack frame is created which limits the access to called function's auto variables. But they are still inside their associated frame and get into action as soon as the called function returns control.

Thus, we can justify the scope and lifetime of auto variables. But, I want to know which data structures are used in the other segments (viz. data, bss and heap) to maintain such scoping. Or is it something else other than the memory layout that controls the scope and lifetime?

It seems, you confuse cause and effect. The scope and the lifetime of a variable is determined by the language standard. The implementation has to ensure, that the standard is met. It might use some memory layout that is handy on a certain platform, but there is no need to do so.

Memory layout with segments as text or bbs is basically a matter of the execution format, not of the language.

That said, one can answer about the most common case: There is no control of scope or lifetime in other "segments". Data and bss (for initialized and non-initialized global/static variables respectively) are for the duration of the process, and the heap is managed explicitly through malloc and free (until the entire heap is destroyed when the process terminates).

I don't know of "Viz", so I can't answer on this one.

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