How are variables stored?

When I declare a variable, say

int x = 8;

What has happened?

The computer knows to find 4 bytes of memory, keep track of the address of the first byte and associate that with the name "x" and place the number 8 in it.

So what is the process it uses to find x when needed? Is there a lookup table where it looks up x and finds the associated address? How does this work please?

Tex

Yes, this table exists (the symbol table), but keep in mind that it only exists while the compiler is generating the executable code. How the compiler translates a reference to x depends heavily in the target platform and the context, especially when optimizations are enabled.

To keep things simple, let's assume unoptimized code for x86.
x86 has a register specifically to keep track of the head of the stack. When a function is entered or returns, the register is decremented or incremented, respectively. Within a single function, the compiler knows in which offsets it will find a given variable. For example, the compiler might allocate stack_pointer + 16 for x. Again, this is unoptimized code. The compiler is free to not even allocate stack space for a variable, under certain conditions.
When you use x in an expression, generally the compiler will generate code that tells the CPU "read the memory location at stack_pointer + 16 and load its contents into register foo". When you assign a value to x, the compiler will generate code that tells the CPU "take the value of register bar and write it to memory location stack_pointer + 16".
If you are interested in the finer details of how computers work, I recommend learning a little bit of assembly language.
Actually, 30 years ago I taught C at the college and then didn't do anything with it for years, then sporadically. I have forgotten alot but most of it is near the surface so I am relearning. And I have a rudimentary knowledge of assembly, at least 8086 back in the day, so I generally understand the principles.

I want to understand how the computer makes the association between x and its address for future use. And therein may be the answer. As I recall functions and their associated variables go onto the stack at the time the function is called and are popped when complete. If that is the case then when int x is declared how does the computer keep track of that fact? When x is used within the function it has to know two things as I see it...one...that x has the address such and such...and two...that address can be found at such and such offset in the stack. But, if so, that association must be remembered somehow, and that is what I don't understand. And what about global variables on the heap that continue their existence throughout the life of the execution of the program?

Anyway I would like to learn more about the symbol table if you have any good sources.

Thank you for the good info.

tex



The compiler does all that - it analyzes the code and generates the appropriate assembly and/or machine code for it. The translation process is very complicated and I haven't bothered to learn how it works, unfortunately.
I'm now reading up on symbol tables and therein lies the answer. The symbol table is like the rolodex for the program which is what I was looking for.

thanks everyone for the help

tex
Topic archived. No new replies allowed.