Engineer999
Engineer999

Reputation: 3955

Where do local variables get stored at compile time?

Maybe i'm missing something obvious, but isn't it during runtime that local variables get placed on the stack when the function containing the variables gets called.

Therefore when the compiler will step through our source code, it will place the operations of the function in the .text segment, but where do the variables get placed at compile time so they can be placed onto the stack at run-time? Thanks

Upvotes: 2

Views: 4029

Answers (4)

Keith Thompson
Keith Thompson

Reputation: 263627

Local variables aren't placed anywhere at compile time.

The compiler generates code that, when executed at run time, will allocate space on the stack (typically; other schemes are possible). The compiler records information about each variable (name, type, size, offset relative to the stack pointer, etc.) and uses that information to generate code that creates, accesses, and finally deallocates the variable.

A technical digression: C doesn't have "local" and "global" variables, or at least the language standard doesn't use those terms. An object has a lifetime (storage duration), which is the span of time during execution when it exists. More or less independently of that, an object's name has a scope, which is the region of program text in which the name is visible. A variable declared inside a function has block scope. It has automatic storage duration by default (meaning it exists only while the containing block is executing), but it has static storage duration if it's defined with the static keyword or if it's defined outside any function. A "local" static variable will be stored the same way as a "global" variable, which is different from the way a "local" automatic variable is stored.

Upvotes: 9

Pooja G
Pooja G

Reputation: 51

Local variables are processed at run time, but global variables are at compile time since their scope is predefined. Global variables are stored in data segment of process memory, whereas local gets stored in stack segment.

It can be cross verified with this piece of code:

int a= 0;
int main()
{
     int b =0 ;
}


After compiling:

       .file   "test4.c"
        .globl  a
        .bss
        .align 4
        .type   a, @object
        .size   a, 4
a:
        .zero   4
        .text
        .globl  main
        .type   main, @function
~                               

Upvotes: 0

paulsm4
paulsm4

Reputation: 121869

With regard to your comment above:

I think you're asking "Q: how does the compiler know how to map a source-level construct (for example,int x) to a run-time location (such as [esp + 2]).

Yes, symbol tables play an important role as the compiler parses the source and generates the binary object file.

From the above link:

Symbol table is an important data structure created and maintained by compilers in order to store information about the occurrence of various entities such as variable names, function names, objects, classes, interfaces, etc. Symbol table is used by both the analysis and the synthesis parts of a compiler.

A symbol table may serve the following purposes depending upon the language in hand:

  • To store the names of all entities in a structured form at one place.

  • To verify if a variable has been declared.

  • To implement type checking, by verifying assignments and expressions in the source code are semantically correct.

  • To determine the scope of a name (scope resolution).

But generating and maintaining the symbol table is just one part of the compiler's job. Here are some good overviews of the entire process:

Upvotes: 0

SergeyA
SergeyA

Reputation: 62613

Neither C, nor C++ dictate compilers where to store local (or global for that matter) variables.

Depending on the compiler, local variables might be stored in the stack, CPU registers, something else or not stored at all.

Upvotes: 0

Related Questions