7
$\begingroup$

As I have understood from all the internet sources I can get to, when you declare and initialize a variable in java, you are allocating this data, say an 8-byte float, in a particular memory cell in the RAM, with the address specified as the row and column number. Now, suppose I want to access this memory location and print the float out. As a programmer, I will clearly not write the binary representation of the memory location of this float for the CPU to process; rather, I will call the variable name I declared before. When this variable name got transformed into binary code and sent to the CPU, how does CPU know what memory location this variable is referencing to? I mean, for that to work, doesn't the memory location of the variable has to be stored directly on the CPU? Because if not, you will have to store the the memory location of the float at some memory cell as well; in other words, when you call the variable, you have to first get from the RAM what memory address is the variable representing and then use that information to go to the RAM to retrieve the float. However, for the CPU to get to the memory address of the memory address of the memory address of the float, it still either has to store it directly on the CPU or has to store it in the RAM, and the same story goes one recursively. What is really happening when you need to call a variable? How to the CPU know the memory address associated with the variable in the first place without referring to the RAM? Because if the CPU is referring to the RAM, then that means it has already gotten the memory address f

$\endgroup$
5
  • 3
    $\begingroup$ The lack of helpful formatting makes your post hard to comprehend. Please take some time to improve the presentation; we collected some advice here. Thank you! $\endgroup$
    – Raphael
    Commented Apr 9, 2016 at 9:52
  • $\begingroup$ You are asking, "how do CPUs and compilers work?" which seem too broad questions to me, and easily looked up in a book. Community votes, please: is this too broad? $\endgroup$
    – Raphael
    Commented Apr 9, 2016 at 9:52
  • 1
    $\begingroup$ @Raphael To me it seems like a reasonable question. It might have some overlap, but it just sounds like a normal thing to ask when you have no idea what compilers do (or don't even know that there is such thing as a compiler). $\endgroup$ Commented Apr 9, 2016 at 13:43
  • 1
    $\begingroup$ @MrTsjolder There are many reasonable questions in the world, only relatively few of which are suited for this here site. $\endgroup$
    – Raphael
    Commented Apr 9, 2016 at 14:28
  • $\begingroup$ @Raphael You certainly do have a point there. Nevertheless I have this intuition that this question does make sense from a computer scientist's point of view... $\endgroup$ Commented Apr 9, 2016 at 14:48

2 Answers 2

8
$\begingroup$

Your assumption that the variable is sent to the CPU is wrong. Every program must be compiled to some machine code (or it will be interpreted, but it roughly comes down to the same thing). The idea of compilation is to translate the human-readable code to a series of bits, which can then be read by the processor. In order to get an idea of what those series of bits do, you could also take a look at assembly which is actually not much more than an encoding of the instruction set of a CPU.

Now the trick is that the compiler builds a so-called symbol table where it keeps track of all variables that have been defined by the programmer(s). Once it knows about all of the variables, it also knows how much RAM it needs (not considering dynamic allocation of course) and thus can allocate a certain space in memory. All variables are then mapped to some piece of memory or if the compiler is smart enough, it might even map certain (temporary) variables only to registers on the CPU to save some memory (not completely sure about that last thing though). The compiler thus creates a series of bits where each variable is represented by the correct register- or memory-address and the cpu does not bother/know about any variables.

For dynamically allocated memory things get a bit trickier, but eventually it comes down to memory addresses being stored in variables which are handled by the compiler again.

I hope I understood your question correctly and did not spread too much nonsense (if I did, please correct me) as I am not an authority whatsoever in this region, but that is roughly my view on these things.

$\endgroup$
1
  • $\begingroup$ Your reference to symbol table has helped me a great deal. In fact, the question arose when I was asking my instructor, (a second CS class using java), why can't we just declare variables runtime instead of building all the linked lists, which overcome the limit of a fixed-length array in Java, to improve efficiency. Now I see why: It is because Java compile and store the variables in a fixed-length array in a sense, so declaring variables runtime cannot happen unless we use data structures other than array, such as linked list. $\endgroup$
    – Kun
    Commented Apr 9, 2016 at 14:19
2
$\begingroup$

The cpu actually doesn't call the variable, but the address of the RAM. The random-access-memory has vertical (column) and transverse (row) addresses. The cpu sends the address values of vertical or transverse or both fields to a register, and the register will drive that values to the I/O variable data. But a "high-level" programmer doesn't want to remember those addresses. So the simple solution is use the compiler of another computer to function vary the name with the refer address. You're confusing between high-level reality and low-level reality logic.

$\endgroup$
1
  • $\begingroup$ Nice to see that your grammar shows significant improvements. $\endgroup$
    – peterh
    Commented Sep 10, 2017 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.