How does a program store variables?

32 Views Asked by At

When I declare a variable in, let's say C:

int x;

The compiler allocates memory, and assigns a memory address to that variable.

When I reference the variable again, the compiler generates some machine code that includes said memory address.

x = 5;

But how does the compiler "know" what the memory address belonging to x is? Wouldn't the compiler need to store the address itself somewhere and thus create a vicious cycle of consuming more and more memory to store the addresses? What am I missing?

0

There are 0 best solutions below