What is a pointer? A pointer is a variable in a program that holds the
address of a data string in memory (RAM). C++ allows you to control
and manipulate expanded memory addresses (RAM > 1069) and its contents
with pointers, combined with it's object-oriented concepts, C++ is
an extremely powerful language.
A pointer holds an address in memory which looks like this "FFFF:00FB".
This address string is much faster to stream thru the system BUS than a
large document such as this. Imagine a group of documents' names were
renamed with the same names but the wrong documents. It would be faster
to rename the files than to make a blank document, paste a document into
the blank document, paste the correct document into that document, then
fill in the other document with the blank or temp document, and so forth
until the files were in the correct order. Mapping memory addresses with
pointers and re-ordering them is equivalent to mapping hard-drive addresses
(files) and re-ordering (re-naming) them.
When sorting data such as names in a database, the sort procedure is more
efficient when swapping memory addresses than swapping actual data content.
Imagine 4 boxes containing names that need to be alphabetized:
1 Smith, Sue
2 Harris, Greg
3 Miles, Brandon
4 Wilson, Jason
Since the above database example uses short names, this may seem pointless
given the power of today's computers but a programmer should make all
reasonable efforts not to assume the power of the machine the program will
be ran on.
If we were going to sort the above names, we would have to create a
blank box to hold each name while the destination name is being transfered
into the destination box, that name would then be transfered to the
box that the moved name came from. This can be a large amount of data
streaming from RAM to the processor, processed, and streamed back to the
new location in RAM (hence the importance of BUS speed). If this data being
swapped were large (2000Kb strings), this would be extremely inefficient.
By using pointers to sort the data, the pointer variables would represent
the correct order when accessing the data for print, etc..
Here is a good example: A warehouse. You have an inventory list and a
warehouse full of widgets (I learned that in accounting class!). What is
the best solution if you found out that the inventory location info does not
correspond to the items' locations on the shelves. Would you move the widgets
to match the printout or re-order the printout to match the widgets' current
locations? The printout of course (unless things are disorganized, but that
does not apply here so forget I mentioned it). That is how pointers work in
RAM. You reorganize the pointers then when finished, rewrite the database in
a streaming fashion (following the new pointer addresses to the next line of
data to write), not in a defragment fashion, moving the data around until it
is in the proper order, the system bus will be taxed just as your warehouse
workers' backs would hurt after reorganizing widgets (imagine if your workers
were union, that is like an ATARI system bus).
Since we are talking about sorting, the bubble sort algorithm may help
clarify 'moving' (variable swapping) data in RAM:
# Bubblesort Algorithm.
variable data = warehouse_inventory_UPCcode_array
variable swapped = FALSE
do (
variable swapped = FALSE
for variable i=0 to data.length do
(loop, counting from first widget thru last)
if data[i] > data[i+1] (
(out of order, swap!)
variable tempholder = data[i]
data[i] = data[i+1]
data[i+1] = tempholder
swapped = TRUE
)
)
) until NOT swapped
The above algorithm swaps data around until all contents are sorted. You
can see that in the middle of the code, the tempholder variable holds
the current box of data while the next box of data in ascending order is
inserted into into the current box, the next content box is then filled
with the tempholder variable which is holding the other box's previous
contents.
When you run the program, what is happening is the tempholder variable
is a location in RAM (only the runtime kernel or RAM registers know where
the location is). Each time we change the value of these variables, the
computer is moving data around in RAM to each variable's respective address.
This takes a few nanoseconds but adds up in applied scalable applications
such as databases.
Using pointers, instead of using the raw variables, we could use variables
of type string (plaintext) representing an address in memory. The variable
tempholder could be a string variable standing for '0CF110', when the code
swaps data contents, it will be swapping these small strings that are only
6 characters long, as opposed to say 512 character fields of warehouse
inventory descriptions. When the above procedure is finished, the data
variable will be a jumbled up (reorganized) array of variables representing
addresses in memory, once the sort process is finished, the program would
simply re-write the database pulling data from RAM using RAM addresses
instead of the program's anonymous runtime address assignments used with
plain scalar variables' references.