Friday, September 3, 2010

C++: Pointers - Hey, where do you live at?

I know you were caught off guard at this question... Well, the next few
topics will help you understand what I mean by this question.
For starters, I thought I should point out where I actually live and the
address of my school (UC Riverside) before diving into C++ equivalent.
Geographically speaking, I live in Koreatown in Los Angeles - yes, where
people like myself live. My apartment is few blocks away from Staples Center
(I am a Lakers fan). Some of the attractions near my address include
downtown L.A., Convention Center, Koreatown Galleria and so forth.
Now to my school. My school is located just east of downtown Riverside
(right by the CA State route 60). Actually, State Route 60 divides the
campus between eastern and western halves. The address is 900 University
Ave. Riverside, CA 92507.
Now to the subject at hand: Do you know how to say, "tell me the address of
UCR" in C++? Can you say, "what exactly is at 900 University Ave." at
Riverside in C++ terms? The answer to these questions is a perfect "yes."
C++ does allow programmers to find out what is stored at such and such
memory address - called pointers. A pointer is the memory address of
something e.g. an integer, a string, or to a small table class we've seen
before. Each address stores the location of where a given data lives, and in
case of arrays and vectors, the address of first of this series of items.
This is done through two operators - the 'address of' and
"pointer/dereference" operators (& and *, respectively).
For instance, suppose if I say, "tell me the address of my school and store
it in a string pointer," I'd write:
string school = "UC Riverside";
string * school_ptr = &school;
Quite a few new things... In essence, these two lines are almost identical
to when we declare variables. The first line is obvious: store "UC
Riverside" in a string variable called "school". The key is the line after
that declaration: store the address of the "school" variable in a pointer
called "school_ptr." Whenever one write a star (asterisk) between the
variable type and the variable name, the name is pronounced as "variable
type pointer." Thus the above phrase came out - storing the address of our
string in this string pointer.
Question: When we say:
cout << school_ptr << endl;
What would be the thing that'll be shown to the user? The answer is the
address of the "school" variable. Not just the address of that variable, but
the address of the letter "U" - remember that string is an array of
characters. Then how many memory locations would this school variable use?
The answer is determined by so-called "memory arithmetic" where moving from
one item to next is determined by how many bytes does a given variable use.
In our case, since the string holds twelve characters, it would use twelve
bytes. I'll come back to how this works later when we investigate memory
arithmetic and how many bytes each variable types occupy.
Now we know the address of something, but how do we find out what exactly is
stored in there? This is done through "dereference" operator (*). don't be
confused here: When one declares a pointer, the asterisk is used; here, the
same operator is used to dereference, or find out what's stored in that
particular memory address. So, if we change the above print statement to:
cout << *school_ptr << endl;
It would print, "UC Riverside" on the screen. What a huge difference... (you
may think it is not a huge difference now, but as we go into all sorts of
adventures with dynamic memory, this becomes important).
at least I hope this gives you a brief introduction to pointers - yes,
that's why i asked that question on the subject line. As we go along, you'll
get a chance to talk to "memory manager", find out how he stores static
versus dynamic memory and all sorts of nasty things that he can help you
prevent, such as segmentation (or seg) fault (which requires extensive
coverage).
//JL (UCR)

No comments:

Post a Comment