Thursday, September 16, 2010

C++: What if I want to point to more than one item?

At least the formatting would be okay this time... Thanks to a consultant's
suggestion, I've changed the formatting a bit.

As we've seen, pointers are used to hold the address of a variable to which
it points to. This has one problem though: what if we want to work with more
than one item at a time? To help out with this task, we can use dynamic
memory - a memory that is allocted at runt time. Also, to traverse a set of
items, we can employ memory arithmetic - moving from one item to next
depending on number of bytes that a given item occupies in RAM.

Let's come back to our basic "car" class - where we have model, manufactured
year and company name as member variables. Suppose if you are a Nissan
dealer who needs someone to print a list of Nissans available, and suppose
there are five cars left after a busy day of sale. The programmer (me, in
this case), is thinking of employing pointers to point to Nissan car
objects, but is wondering how to store five cars and have the pointer point
to it.

To help me out, what would be your suggestion? As it turns out, after
noticing that all Nissans share a common attribute - that they are cars -
allows me to employ a dynamic array of cars to help me print out what cars
we have left at our dealership. So, I started to write...

Car * car_ptr = new Car[5];

Remember a similar code from last time? We've just declared a dynamic array
of five cars. The keyword "new" means that we wish to use dynamic memory
(stored on the heap, as opposed to stack, which holds static variables; I'll
come back to it in a second). Just like arrays, we use brackets ([]) to tell
the computer how many items we would like to use - so that we can use index
0 through 4. This is one form of declaring a collection of dynamic memory.
Note that the pointer would point to the first of these elements (index
zero).

But if your goal is to declare just one item and put it in dynamic memory,
we could employ new keyword as follows: Suppose if I have a character called
"C" that I'd like to put it up on dynamic memory, and have a character
pointer point to it. We can write it as follows:

char * char_ptr = new char)"C"); // Hope my syntax was correct...

Or, suppose if I have a double called "3.0" and wish to stored it on dynamic
memory, we can do this:

double * double_ptr = new double(3.0); // I think this is better.

The above two lines just declares a pointer to point to an item on the
dynamic memory (only one item, not an array of items). As you'll learn
later, if you forget about your declaration, a troubling consequence may
occur - especially if you've declared a dynamic array.

Now back to the dealership. What if I want to assign a particular car model
to a specific index? We can use memory arithmetic to help us with this task.
When this is invoked, moving from one item to next is dictated by how many
bytes does a given object use in memory. Common sizes are 4 bytes for an
integer, 8 bytes for a double and 1 byte for characters (strings is a
special case, as it is an array of characters). For example, if I have four
integers (starting at address 100), if I use memory arithmetic, index 1 of
this array would get the address 104, index 2 at 108 and index 3 at 112.

But counting bytes in memory is very tedious (note that RAM addresses are
expressed in hexadecimal numbers - base 16). To make coding faster, we use
subscript operator ([]) to help out with this task. All we need to access a
given item in a set of items is via its index location. For instance, if I
want to access the second car in an array of dynamic cars, we'd write:

Car_ptr[1] = whatever; // remember our discussion on zero-based indexing.

So, the problem of assigning a car model in an item in our car array has
been fixed - and even allowing me to assign the same car model to all of
them has been fixed via for loop, as shown below (pseudo code first, then
C++ code):

Pseudo code:
1. First, find out how many cars are there. Here, we don't need to do that,
since we do know how many there are (5).
2. Use for loop to assign these car objects to the same manufacturer, model
and manufactured year. We would use subscript operator to assign items in
the car object to same values.

In C++, we'd get:

For (int i = 0; i < 5; i++)
{
Car Nissan (2008, "Nissan", "Ultima");
Car_ptr[i] = Nissan;
} // If I remember right.

At least, three quarters of my job is done. The only thing left is printing
it: same principle as assignment code above.

For (int i = 0; i < 5; i++)
{
Car_ptr[i]->print();
}

Ah, yes, I should have gone over the arrow operator. Whenever we access data
that a pointer points to (including member functions), we use arrows (->)
instead of dot (.) operator. For instance, if we have a table class and want
to print its length (given if length variable (an integer) is declared in
there), we'd write:

Table test (3, 5); // Length = 3, height = 5.
Table * test_ptr = &test; // The address of our test table.
Cout << test_ptr->length; // Print the length of this table that the pointer
is pointing at.

Look at the last line. Instead of the above line, the compiler would give an
error if you write:

Cout << test_ptr.length; // Uh oh...

Thus, whenever we work with pointers that needs to access data (for our
classes), we'd use arrows.

One last bit: What if you forget about assigning dynamic pointers and let
the program terminate? The next time you run a program, there might not be
any more room for you to work with data. This is the consequence of memory
leak where a program would forget to deallocate (remove, well, almost)
memory when it is done. To help avoid this problem, we'd use "delete"
operator to return memory locations that we've used back to the operating
system.

Depending on the memory we've used, there are two ways that relies on
presence of subscript operator:
* If you used only one dynamic variable, it is sufficient to write:
delete dynamic_ptr;
* If you've used a dynamic array, then it is CRUCIAL to put subscript
operator, otherwise only the first of these items (index 0) will be
deallocated, leaving the rest of the array "floating around":
Delete [] array_ptr;

So, to clean up our dealership and other dynamic variables we've used, I'd
write:

delete [] car_ptr; // Forgetting the subscript operator produces memory
leak.
Delete double_ptr; // Only one double was used.
Delete test_ptr; // Only one test table.
Delete char_ptr; // Only one character.

I think that's all I can think of in terms of memory allocation (mostly
dealing with dynamic memory allocation) and memory arithmetic. Next time,
I'll discuss the self-assignment (this->) operator, as well as more on stack
versus heap.
// JL

Sunday, September 12, 2010

C++: Pointer exercise 1

Before I dive into rants on memory allocation, I thought I should pose a
small challenge for you - to make sure that you haven't forgotten about
working with basic classes:
This exercise is divided into two parts:
1. Create a computer class to store two string variables (company name and
model), as well as a double for storing CPU speed (in gigahertz (GHz). The
default constructor would set the computer model and brand name to null
string and CPU speed to 0. The parameterized constructor would assign
variables data from user-supplied arguments. That's it for now.
2. Create a computer variable named "my_computer" and a computer pointer
called "computer_ptr" that'll point to the address of my_computer. Then
create another computer object called "Apple", this time assign it the name
"Apple (brand name) Mac Book Pro (model)" and set the CPU to 2.13 GHz. Then
assign a new computer pointer called "Apple_ptr" to our Apple computer
object. Then assign computer_ptr pointer so that it now points to apple_ptr.
We'll revisit our Apple notebook later when discussing about displaying it
as part of a computer store shelf i.e. memory arithmetic and how to use
this-> operator.
// JL

C++: Declaring pointers

Last time we examined what a pointer is and a basic declaration of a pointer
- address of a variable. Let's discuss other ways of declaring pointers.
Along the way, I'll introduce you to memory arithmetic (especially in
regards to the last method of pointer declaration).
There are four or five ways of declaring a pointer (remember that in order
to declare a pointer, it needs the asterisk operator between the ptr name
and the data type). The first way is, of course, declaring the pointer to
hold the address of a known variable. For instance, if I store my age in an
integer variable and have an age pointer point to my age variable, I'd
write:
int age = 20;
int * age_ptr = &age;
Sound familiar? This is exactly the same declaration style I've used on
previous post, except that I'm now using integer pointer (as I've said
earlier, pointers can point to anything). Just like last time, the "age"
variable would hold my age right now (20) with age_ptr holding the address
where my age variable lives (not exactly "lives," but more towards
"located."). So, if we verify my age using cout, it'd be (like last time):
cout << *age_ptr << endl;
Remember what we've discussed earlier - just saying "age_ptr" would give us
the address of something, not the actual guy, thus I wrote dereference
operator along with age_ptr. On the monitor, my age (20) would be displayed.
But suppose if I have a friend who wish to store his age as a pointer and my
current age_ptr be modified (reassigned) to point to his age pointer? The
answer is perfect "yes." Pointers can point to other pointers, just like
values from one variable can be assigned to another. Let's see how we can do
this:
int friend_age = 24;
int * friend_age_ptr = &friend_age;
Here's the tricky part:
my-age_ptr = friend_age_ptr;
Wow, something we haven't seen before, but once you examine it carefully,
you'd understand that the above statement is perfectly valid. To help us
with a better illustration, suppose the address of my_age variable was 0001
and my friend's age variable address was 0002. Here's the map of the
variable assignment:
* my_age_ptr was initially assign to address 0001 (my age).
* Then friend_age_ptr was assigned to address 0002 (my friend's age).
* using the above map, we reassigned my-age_ptr to point to friend_age_ptr -
the address pointed to be my_age_ptr was changed from 0001 to 0002.
Now, if we print my_age_ptr (via dereference operator), we'd get my friend's
age instead (by the way, that friend of mine is my programming consultant).
But what if we have a pointer but don't know the variable to which it should
point to? In this situation (particularly when writing class constructor and
if part of the encapsulated variable is a pointer), then we can use a
safeguard - assigning the pointer to the address of zero (or null). A
typical declaration is:
data_type * pointer = NULL; // (notice that we need to capitalize the word
"NULL").
This is useful if you need to create a known pointer but doesn't know where
it should point to, or when creating default constructor for a class that
has a pointer variable as part of encapsulated data. But this declaration
has a cost: a likelyhood of a "fight" between you and your computer over not
being able to read from address of zero (one form of segmentation fault;
I'll come back to it in more details later). In order to avoid this, be sure
to assign a valid variable address to the pointer once the variable was
declared, like this:
double * double_ptr = NULL;
cout << *double_ptr << endl; // BAAAAAAD!
// Computer says, "Hey, you can't read from address of zero!".
double cosine_30_dg = 0.866; // Cos 30 = sqrt(3/2).
double_ptr = &cosine_30_dg;
cout *double_ptr << endl; // Thanks.
Now, when we print this, it shows 0.866 (cosine of 30 degrees, which, when
squared and multiplied by square of sine of 30, produces 1 (a well known
trig identity)).
The last method is something I need to come back in full detail later, as it
is related to the next post on memory management and dynamic memory, but
just want to show you how it can be done:
Suppose if I have a collection of data (like a vector) and want to store it
somewhere but don't want to bother with creating another variable just to
hold this collection. A neat way of accomplishing this task is using
so-called "dynamic memory" where we can work with it at run time. Unlike
so-callee variables we have worked with, this memory requires special
attention, as doing wrong things at wrong time could result in this memory
not being usable by other programs (called memory leak where a program would
use more and more memory but not relinquish memory space that it no longer
needs). Also, this memory does not have a name (actually, a variable name
does exist, but it is not that apparent yet).
To work with dynamic memory, we need pointers and two new keywords - new and
delete. I'll come back to meaning of these keywords in a sec (perhaps next
time), but to give you an idea of coding it, let's create a pointer that
stores five dynamic integers i.e. dynamic memory of five integers:
int * dynamic_ptr = new int[5];
Almost similar to how we'd declare an array of something as we've seen
earlier. Here, the pointer (dynamic_ptr) points to index 0 (first number) of
our array.
To tell the computer that we don't need this dynamic array anymore, we'd
use:
delete [] dynamic_ptr;
Note that we use brackets ([]) to delete the entire array. We'll examine the
operation of this task in more detail as we continue our pointer discussion
with dynamic memory allocation (coming up shortly), but a bit of review
exercise is in order...
// JL

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)

Wednesday, September 1, 2010

C++ and other topics: what now...

Hi,
At this point, I'm taking a short break from writing things about C++. But I
hope to start writing about other useful goodies by tomorrow - with a new
topic at hand. Some of the upcoming shows include a detailed word on
pointers, how to derive specialized objects from a base class, notes on
other OOP topics (polymorphism, function overriding, etc.) and memory
management (the big three). The last topic for C++ show would be a summary
of my experiences as a student programmer transitioning to the "real core"
of computer science - data and how we can work with it, and a general
overview of data structures - the show coming after C++.
//JL

What a surprising news: downfall of WP7 and rise of Android for screen readers...

Code Factory, the developer of Mobile Speak series of screen readers and its
accompanying accessories, just announced that they are working on a screen
reader for Android. In addition, CF just aannounced that, due to some
programmability changes, the initial version of Windows Phone 7 will not be
supported. I guess it's time for me to learn more about Android and how it
works...
//JL