duleshi
duleshi

Reputation: 2014

When referenced by name,where is the name stored?

For example, after we define a variable:

int a=2;

we can use "a" as 2 afterwards.
At the time when I first learned c/c++, I took it for granted.
But after I have learned the concept of "pointer" and the address of variables, I am confused.

If int* p=&i;, where "i" is an int. I know that p have the address of i, so we can use *p to get the value of i. But if we go further, how is "p" accessed? It seemed that p must be referenced by name,right?

By now, to access a variable via its address seems more natural to me and easier to understand.
But now , I'm a confused about the mechanism when using the most simple way to access a variable.
As in the case of int a=2; , where is the name "a" stored?

Why if we use "a", it is equivalent to the behavior of accessing the memory where the "a" or the "2" is stored?

Upvotes: 2

Views: 277

Answers (3)

Michał Górny
Michał Górny

Reputation: 19243

Here a is just a symbolic name. If it's a local variable, it ain't stored anywhere. Compiler simply uses it during the compilation phase, referencing the actual value (which can be on the stack or in register), then discards.

If you looked at the assembly generated by compiler, you'd notice that a isn't appearing there (or maybe in comments). Compiler will fit your variable somewhere, and just use that location afterwards (like the eax register on x86).

If you looked at an LLVM assembly (which is quite interesting), you'd notice that the compiler just treats your variables as @1, @2, @3...

On the other hand, if a would be a global variable (and a non-static one), the name would actually be used in the symbol table to reference the variable. But it'd be the other way around — variable would be placed somewhere without the name, and the symbol table would map that name to the location so that other programs could find it.


As a side note: if the program is compiled with debug data, the name a is stored there so that the debugger could display it to help you understanding what's happening.

Upvotes: 9

Superman
Superman

Reputation: 3081

All symbols like "a" and "p" will be converted to addresses after the compiling and linking process. The symbols are just for the functions to use them by name. It's like a tag for the memory address. Even though "p" holds the address of "a", the pointer "p" is also changed to an address by the compiler.

Upvotes: 2

Björn Pollex
Björn Pollex

Reputation: 76828

This is called abstraction. How and where a is stored is deliberately not specified. The compiler will take care of it for you.

In practice, if a is a local variable inside of a function, it will be store in the stack-frame of that function at the time of the call. The layout of the stack-frame is determined at compile time, and does not change after that. In effect, the compiler will generate a relative address for a (relative to the start of the stack-frame), that will be converted to an absolute address at the time the function is actually executed, and the starting-address of the stack-frame is known.

Upvotes: 3

Related Questions