In C, let's say you have a variable called variable_name
. Let's say it's located at 0xaaaaaaaa
, and at that memory address, you have the integer 123. So in other words, variable_name
contains 123.
I'm looking for clarification around the phrasing "variable_name
is located at 0xaaaaaaaa
". How does the compiler recognize that the string "variable_name" is associated with that particular memory address? Is the string "variable_name" stored somewhere in memory? Does the compiler just substitute variable_name
for 0xaaaaaaaa
whenever it sees it, and if so, wouldn't it have to use memory in order to make that substitution?
This is what's called an implementation detail. While what you describe is the case in all compilers I've ever used, it's not required to be the case. A C compiler could put every variable in a hashtable and look them up at runtime (or something like that) and in fact early JavaScript interpreters did exactly that (now, they do Just-In-TIme compilation that results in something much more raw.)
Specifically for common compilers like VC++, GCC, and LLVM: the compiler will generally assign a variable to a location in memory. Variables of global or static scope get a fixed address that doesn't change while the program is running, while variables within a function get a stack address-that is, an address relative to the current stack pointer, which changes every time a function is called. (This is an oversimplification.) Stack addresses become invalid as soon as the function returns, but have the benefit of having effectively zero overhead to use.
Once a variable has an address assigned to it, there is no further need for the name of the variable, so it is discarded. Depending on the kind of name, the name may be discarded at preprocess time (for macro names), compile time (for static and local variables/functions), and link time (for global variables/functions.) If a symbol is exported (made visible to other programs so they can access it), the name will usually remain somewhere in a "symbol table" which does take up a trivial amount of memory and disk space.
Variable names don't exist anymore after the compiler runs (barring special cases like exported globals in shared libraries or debug symbols). The entire act of compilation is intended to take those symbolic names and algorithms represented by your source code and turn them into native machine instructions. So yes, if you have a global
variable_name
, and compiler and linker decide to put it at0xaaaaaaaa
, then wherever it is used in the code, it will just be accessed via that address.So to answer your literal questions:
The toolchain (compiler & linker) work together to assign a memory location for the variable. It's the compiler's job to keep track of all the references, and linker puts in the right addresses later.
Only while the compiler is running.
Yes, that's pretty much what happens, except it's a two-stage job with the linker. And yes, it uses memory, but it's the compiler's memory, not anything at runtime for your program.
An example might help you understand. Let's try out this program:
Pretty straightforward, right? OK. Let's take this program, and compile it and look at the disassembly:
See that
movl
line? It's grabbing the global variable (in an instruction-pointer relative way, in this case). No more mention ofx
.Now let's make it a bit more complicated and add a local variable:
The disassembly for this program is:
Now there are two
movl
instructions and anaddl
instruction. You can see that the firstmovl
is initializingy
, which it's decided will be on the stack (base pointer - 4). Then the nextmovl
gets the globalx
into a registereax
, and theaddl
addsy
to that value. But as you can see, the literalx
andy
strings don't exist anymore. They were conveniences for you, the programmer, but the computer certainly doesn't care about them at execution time.Yes.
Yes. But it's the compiler, after it compiled your code, why do you care about memory?
All variables are substituted by the compiler. First they are substituted with references and later the linker places addresses instead of references.
In other words. The variable names are not available anymore as soon as the compiler has run through
A C compiler first creates a symbol table, which stores the relationship between the variable name and where it's located in memory. When compiling, it uses this table to replace all instances of the variable with a specific memory location, as others have stated. You can find a lot more on it on the Wikipedia page.