P O I N T E R     V A R I A B L E S

INTRO:
Pointers are remarkably similar to reference variables. In fact reference variables are the same thing, just easier to use, but less dynamic. If you haven't read my little ditty on references variables or if you don't know what they are then click here. Pointers are the number one cause of pain with C and C++ programmers. You can do so much with them and at the same time you can fall into a deep pile of shit if you screw up. This is a revised document. If it doesn't help you can view the old one here.

WHAT ARE THEY?
Pointers are data types. They store memory addresses. That's about it. Doesn't sound too complicated and its not but when you get into heavy use of them, little screw ups can lead to heavy crashes! In this document I will only deal with pointers to simple data types (integers and characters mostly).

MEMORY ADDRESSES
Before I get into how to use pointers, you're going to need a basic understanding of memory addresses. Now these can differ when going to different platforms so I'll try to keep this pretty generic (yeah, like I can do anything right ).

A memory address is a location within your computers memory. Its where something is. And each one of your variables has a memory address of its own. Which means that at that address in memory is the value of that variable. Say for instance you have a single integer named x and it has a value of 654. Let's pretend that x's memory address is "0x5FA70" (hexidecimal). Well, at the location "0x5FA70" in your computers memory is the number 654. Still, don't see it? Okay, well let's just say for the sake of example that your computer's memory looks like this:

memory addressvalue
0x5FA62...
0x5FA66...
0x5FA70654
0x5FA74...
0x5FA78...

See, now at the memory address "0x5FA70" is the number 654. I put "..." in the other boxes because we don't know what's at those memory locations. On a nerdy note, I wrote the memory addresses in hexidecimal because its easier to represent them that way. Also, the addresses in this depiction increase in increments of 4. That is because the size (in bytes) of an integer is usually four (unless you're working with a crummy ol' 16-bit compiler). Actually you can try out the following program to see how big (in bytes) your typical integer is with your compiler:

/* -example-[size of typical integer]-------------------------------- */

#include <iostream.h>

int main()
{
  int x;

  cout << "The size of integers on this compiler is " << sizeof(x)
       << " bytes." << endl;

  return 0;
}

/* ------------------------------------------------------------------ */
After compiling and running the above program you'll know the default size of integers on your compiler. So for instance if it told you that they were only two bytes long then you could change my example listing of memory to increase by two rather than four. So instead of going "0x5FA62", "0x5FA66", ... it would go "0x5FA62", "0x5FA64", "0x5FA66" ...

Remember, every single stinking variable has a memory address. It has to keep whatever you put into it somewhere. So when we stick that 654 into x it puts it at that memory address. The memory address is the place where that variable keeps its value. I suppose you might be able to say its like how everyone on the internet has an email address. Hmm, better yet. Since a memory address is a location ... if you were a variable then the place you were sitting/standing would be your memory address. And what you are is the value that is put there.

GETTING A VARIABLE'S ADDRESS
At some point we'll need to know the address of a particular variable (get to WHY later). To do this we place an ampersand in front of the variable. When we do that, it is interpreted as the memory address of that variable rather than its value. Its like when/if you're a variable, pushing you aside to see where you're standing rather than to look at what/who you are. So in the case of &x we would see "0x5FA70" rather than 654. And how does this work, you ask?

Let's say we wanted to output the memory address of x. Run this short program:
/* -example-[memory address of 'x']---------------------------------- */

#include <iostream.h>

int main()
{
  int x = 654;

  cout << "The value of 'x' is " << x << ", its memory address is "
       << &x << endl;

  return 0;
}

/* ------------------------------------------------------------------ */
So by putting that ampersand in front of x we're telling the compiler, "we don't care what the hell it has, we want to know where it is!" You'll get varying results from this program, for example when I compiled it through DJGPP I did get "0x5FA70".

By this point you should have a very basic understanding of memory addresses. Enough so you kind of know what they are, and what's in them.

MORE INFORMATION
Memory addresses may stay basically constant on one machine. For example, on your computer you may always have usable address space at "0x5FA70". The values at memory addresses, however, are not fixed. When you run your program the variable x may very well be at that address. However, when you're program completes and exits (or crashes ), the address becomes available again. The next time you run your program x could possibly be a memory address megabytes away!

Did you ever declare a variable and then view its contents before actually putting anything there? Well, try it out and you'll probably find that there's garbage data there. Garbage as in useless! When a process or program ends or at least stops using a part of memory, it does not automatically get cleared. This means that when another program comes along to use the same space, there will generally be crap there. That's why its a good idea to remember to initialize your variables before using them and never assume that the value will be zero. Some compilers will automatically generate code to reset variables when you declare them - do not depend on this under any circumstance.

AN ANALOGY
So how about an analogy for this. Let's take a few houses. Each house is a memory address, the value in the house is the person that lives there. Note that people will move, but the houses always stay there. It might look something like this (relating to an earlier example above):

house analogy

Think of "647" as the person that lives at address "0x5FA70". x could be the car that gets you to see that nice person, "647". If you put an ampersand(&) in front of x you are looking for the house address and you could care less about who's inside.

DOING SOMETHING WITH THAT ADDRESS


** Still unfinished! **

The End (for now, *muh ha ha ha*)

Page Content & Design ©1998
By Neil C. Obremski

home

prfce

loop

refs

ptrs

struct

class

array

rsrc

site

indx
Wed May 12 21:45:26 1999
C O N T A C T   F O R M
Name:
E-Mail:

 
 
Message:
 
 
Newsletter: (un)subscribe from my newsletter
C O N T A C T
You may use the form to your left to contact me if you have unanswered/unsaid questions, complaints, contributions, comments or just want to bitch. I recently made guidelines to follow before writing me. Please respect these, thank you.

Ashrak DeadEye nobremski@hotmail.com
home 652447
╧ceGoblin icegoblin@hotmail.com
school 5676542
Masta masta@cyclone7.com
business none