In Da Programming: VARIABLES

4 of the SEXTUPLE— name, address, value, type

Image for post
Life itself is full of variables that we interact with daily.

As it was promissed from In Da Programming: Names post that next post will be dedicated to the honor of variables, and here we go.

Regardless of what programmer usually thing and how many miss descriptions of variables are in the internet, the truth is that it is an abstraction of a computer memory cell or collection of cells. In previous sentence, I did puposefully turned the camera to programmers for a second, because most of the time they think of variables as names for memory locations, however the reality is much more broad and rich.

To start with small history, would kill nobody. In old but gold days, when machine language was a king, everything was absolute numberic memory addresses. Thanks to god, assembly destroyed this monarchy and add readability and writability to the programming. Basically, absolute numeric memory addresses now was replaced with names.

A variable can be also called as a SEXTUPLE OF ATTRIBUTES(I know this sound like 50 Shades of Gray, but they are totally different things). Let’s now see what they are:

  1. Name
  2. Address
  3. Value
  4. Type
  5. Lifetime
  6. Scope

You may thinks that from a simple a word how we created a mountain, but in deed all of the above written instance brings clalrifications to the subject even further. Some of them can be explained in a sentence and some of them would need more time to eloborate. In this post we will cover name, address address, value and type. Let’s start.

Name

In previous post (if you haven’t yet read I just drop the link here) we talked a lot about names and now I can say that variable names are the most common names in programs and the rest you know from the previous talking.

Address

Lets begin with an official description.

The address of a variable is the machine memory address with which it is associated.

However, it sometimes this association may not be as simple as it looks. It is proven that in many languages, it is even possible for the same variable to be associated with different addresses at different times during the execution of the program. Let me bring an example to make it even clearer.

Example: let’s say that you have a subprogram and it has a local variable which is allocated from the run-time stack when the subprogram is called. In such situation, different calls may result in that variable having different addresses and this process simple can be comprehended as different instantiations of the same variable.

The address of the variable from time to time is called l-value and the reason is usually explained as below

the address is what is reauqired when the name of a variable appeards in the left side of an assignment.

As we above discussed how a variable can have different address each time, now it is time to talk about the variables which has same addresses. By the way this is called aliasing, and those variables are aliases.

Pros: Aliasing is a hindrance(I don’t know what this means just stole from the book, just kidding, read it here) to readability, because it allows a variable to have its value chnaged by an assignment to different variable.

  • Example: let’s think you have 2 variables total and sum, and luckily they are aliases which means any change to the value of total will also change value of sum and this is true vice versa.

Cons: It is always must be remembered that this 2 variables are pointing to the same memory address, and now think that you have not 2 but 100 of such alises and you come back from a holiday. Yay, know this might be a nightmare.

There are many ways of creating aliases. For example, in C and C++ union types can be used to achive that.

In addition, 2 pointer variables are aliases when they point to the same memory location. The same is true for reference variables. This kind of aliasing is simply a side effect of the nature of pointers and references. When a C++ pointer is set to point at a named variable, the pointer, when dereferenced, and the variable’s name are aliases.

Type

Type is mainly important to determine the range the values variable can store and the set of operations that are defined for values of the type.

  • Example: the signed int type in Java, the inclusive lower bound is -2147483648 and the inclusive upper bound is 2147483647. The type allows arithmetic operations such as addition, subtraction, multiplication, division, and modulus.

Value

The value can easily understood as the item stored in variable container but if you wish more pro description read the below one

The value of a variable is the contents of the moemory cell or cells associated with the variable.

The cells that are talked above are more like abtract cells rather than physical. The physical cells, or individually addressable units, of most contemporary computer memories are 8-bit units called bytes which is too small for most program varibles(int itself takes at least 4 bits).

  • Example: although f loating-point values may occupy four physical bytes in a particular implementation of a particular language, a f loating-point value is thought of as occupying a single abstract memory cell.

The value of each simple nonstructured (int of 4bytes) type is considered to occupy a single abstract cell.

Therefore, we can say that the term memory cell will mean abtract memory cell.

Value has a second name as we saw in address as well. It is also called r-value and this is because it is what is required when the name of the variable appears in the right side of an assignment statement.

Last bullet point is that to access the r-value, the l-value must be determined first.

REFERENCES

  1. Sebesta, R. W. (n.d.). Names, Bindings and Scopes. In Concepts of Programming Languages (12th ed., pp. 200–202).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store