-1

How are variables abstractions for the memory cells of the machine? Is it because you can change the value stored in the memory cell by assigning a different value to the variable associated with the said memory cell?

Doc Brown
  • 206,877
blah
  • 21
  • 1
    Your question could be improved by telling us where you got that statement from, and which research you did so far. – Doc Brown Nov 19 '17 at 15:35
  • I see, I'll keep that in mind next time. Thank you for taking the time to answer. – blah Nov 21 '17 at 16:06

1 Answers1

3

In many programming languages, variables provide primary two different abstractions:

  • first, they provide a location for holding some data, abstracting from the physical place in memory where those data is really stored
  • second, they give that location a sensible name

To explain the first point a little bit: a variable holding a 32 bit integer (for example) can be seen as 4 bytes of contiguous, fixed memory of a machine, that is a useful mental model which fits for lots of programming languages and many contexts. In reality, however, the compiler, runtime environment, operating system or hardware maps that data somewhere else, optimizes it away or moves it around between different places in memory or different memory layers. This is even true for "read-only" variables for which you cannot "assign a different value" after their first initialization.

The second abstraction is hopefully self-evident. Memory locations don't have sensible names, they have addresses. CPU registers often have names, but these names don't have any relationship to the semantics associated with the data in context of a specific program. So variable names abstract from those addresses or CPU register names.

Doc Brown
  • 206,877