Tech Tuesday: Data Types (Part 2)

Last Tech Tuesday we got started on data types as port of the ongoing cycle on programming.  This post will not make sense without reading Part 1, so if you haven’t you should go back and do that now.  We encountered the problem of “adding” two variables where one holds a number and the other holds some text.  Today we will dig a bit deeper into this problem.

Let’s start by looking at the question what it even means for a variable to hold a value.  As I explained in the post on literals, constants and variables, the variable is a name that we can use to refer to its value and the value can change as our program executes.  At the heart of any way of doing this will eventually be an address in the computer’s memory.  It’s at that address that we find a binary representation of the data in question.  For instance, for text we might find a sequence of bytes where each byte represents a character encoded using ASCII with eventually a byte containing 0 (null) to mark the end of the text (that in any case is how it worked until we needed multi-byte characters for internationalization). For a whole number (an integer) we might simply find the binary version of that number.

Separately, we might have a kind of internal “phone book” or address table where for each variable name we keep track of the address in memory at which the value for that variable lives. So coming back to our example from Part 1 where we had:

The “phone book” here would have two entries.  One for the name “a” and one for the name “b” roughly as follows

Where 1000 and 1002 are addresses in the computer’s memory.  When we look at the actual memory locations, we might see something like the following

Loading...
highlight
Collect this post to permanently own it.
Continuations logo
Subscribe to Continuations and never miss a post.
#tech tuesday#programming#data types