How a variable and its reference is stored and accessed in computer memory?
Asked Answered
R

1

5

How is a variable stored in a computer memory ?

For example: when we specify

int x = 15;

Typically the computer in its ram allocates a 4 bytes chunk of memory, storing the value 15 in the form of 0's and 1's in the allocated 4 bytes and x refers to the address of the 4 bytes allocated memory.

Now, my doubt is how x refers to the memory location. Where and how the reference is stored ?

When we use x how it knows to refer to that memory location.

Is this different for different languages like java and python ?

Is this different for different data types ?

I shall be glad if you can make videos or blog to clarify this.

Ramirez answered 24/12, 2018 at 5:58 Comment(0)
C
7

Typically the computer in its ram allocates a 4 bytes...

The whole sentence is completely correct. Only, there are different types of allocation. The simplest one is that the computer has, in a certain moment, an amount of free and contiguous memory. When a program starts, all that (free and contiguous) memory is given to the new program. It is completely responsibility of the program to manage the memory, and to not write outside that zone of memory. This is how DOS (real mode) works - very simple. The program (well, its runtime) is only informed about the starting and ending addresses of the zone of memory. It "maps" some "base" register into that ram, so as, for example, address 0 refers to the start address, where perhaps the program knows the variable X resides. More advanced operating systems go beyond, for example with mechanisms to block a program trying to read or write outside its allowed ram. Even beyond, more sophisticated OS's can give a program some ram initially, and then more ram when the program asks it.

Now, my doubt is how x refers to the memory location. Where and how the reference is stored ?

Typically the reference is not stored at all in the (compiled) program. What you (the programmer) call "X", for the program simply becomes an address. First variable address 0, second variable address 4, and so on. Debug symbols, when not stripped off, keep track of the "high level" names given to addresses; but they are used by debuggers and are not technically part of the "program".

When we use x how it knows to refer to that memory location.

Let's write a simple C program:

int x = 15;
int y = 20;
int s;

s = x+y;

The compiler sees "int x" and assigns to x the address 0. Then sees "int y" and assigns it address 4. Then address 8 to "s". Of course these addresses are remembered through the whole process of compilation. Meanwhile it sees those "x=15" and "y=20", and so outputs these instructions:

"store an integer of 4 bytes, value 15, at address 0"  
"store an integer ..., 20, at address 4"

In the end it sees "s=x+y" and outputs this:

"take in the left hand the int (4 bytes) value at address 0"
"take in the right hand the int value at address 4"
"pour the left hand in the right hand"
"pour the right hand in 4 bytes at address 8"

As you see, there is no more x y or s, only addresses (this is a very simplified explanation).

Is this different for different languages like java and python?

Yes, but not too much. The name of a variable always refer to an address. Pure interpreted languages have to keep track of the name, because there is no separation between compilation and execution, and midway languages like java (I say midway because java is both a compiler and an interpreter) can do even more complicated things.

Is this different for different data types?

For simple types, no. In the C language, if we don't consider pointers and heap, all the variables are treated the same, only their length changes. An int can be 4 bytes long, a char one byte, an array has a length specified by the programmer, but the variables always refer to the first location of the buffer containing the value. The compiler keeps, for every variable, its address, its type, its length, so it can know what to do (what instructions to compile) when it encounters the variables names in the source.


Edit for "can you tell me how exactly the "x becomes 0" part happens in a computer during compilation"

It is very simple in concept. The compiler reads the text of the source. Every time it encounters a variable declaration it assigns it the first free address, then increment that by the size of the variable. At the begin, this address is 0. After the compiler reads a "int x;", the address of x becomes 0 and the "current" address is incremented (for an int, probably 4 or 8). Variable names, their type and their addresses are kept, and form a lookup table. This table is used to check that you don't declare an identifier twice, and is used to know where to read and write when the source refers to an already declared variable. If the source references an undeclared variable, the compiler complains because it doesn't find the variable in the lookup table. Note aside: you posted a link to a video which talks about pointers; pointers are also variables, but they introduce other concepts. What I said about addresses applies to pointers too, in the sense they are simply variables, but their value is used differently - and I don't understand why you insist on them.

Casework answered 24/12, 2018 at 7:12 Comment(6)
"take in the left hand the int (4 bytes) value at address 0" How does the compiler know to which address to start from ? Does it maintain a lookup table for this in runtime ? see this video: linkRamirez
@Ramirez absolutely not, not in runtime. The lookup table is there during compilation only. You can think as the compiler renames the variables: x becomes 0, y becomes 4, s becomes 8. Then during runtime the programs operates on the variables using their new name, "0", "4", "8" and so on.Casework
understood. But can you tell me how exactly the "x becomes 0" part happens in a computer during compilation.Ramirez
really helpful explanation, however need some clarification for the variable lookup table. This lookup table is formed during compile time or runtime ? In which part of the memory is this table stored, stack, heap.. ?Ramirez
I thought it was clear... the lookup table does not exist in runtime, hence it is built and used only during compilation. As to where, probably the heap because of its capacity to shrink/grow.Casework
@Ramirez post another question to SO: a question, a topic. Moreover, messages should not turn into a chat.Casework

© 2022 - 2024 — McMap. All rights reserved.