1

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?

6
  • 2
    It's a potential pitfall to confuse some of the standard-mandated semantics with the details of particular a particular implementation. Can you clarify your question a bit? What are you really trying to understand here? Commented Apr 30, 2013 at 4:43
  • To elaborate on what @Carl said, take this quote: "auto variables are stored in the stack." Well, yes, and maybe not. In all implementations that I know of, variables with automatic storage duration are allocated on a stack structure. However, the C language says nothing of a stack. They could be implemented in other ways, a stack just happens to be a good one. Commented Apr 30, 2013 at 4:55
  • "Or is it something else other than the memory layout that controls the scope and lifetime?" compiler might embed calls to destroy the auto variable after its scope ends to conform with the standards. but to us we are guaranteed that the scope ends when and where standards says so. Commented Apr 30, 2013 at 5:29
  • @CarlNorum I wanted to know what data structures are implemented in the bss and data segments for storage and retrieval of data. I wanted to know if it is possible to justify the scoping as in case of auto and register variables. Commented Apr 30, 2013 at 8:44
  • @EdS. So, does it mean the actual implementation need not be like auto should use stack segment and static bss,data... etc? Commented Apr 30, 2013 at 8:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

viz. is a latin abbreviation meaning, roughly, specifically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.