20.1 Pointers, Structures, and Classes

I l @ ve RuBoard

Structures and classes may contain pointers, or even a pointer to another instance of the same structure. In the following example:

 class item {      public:         int value;          item *next_ptr;  }; 

the structure item is illustrated by Figure 20-2.

Figure 20-2. Item
figs/c++2_2002.gif

The operator new allocates storage for a variable and returns a pointer. It is used to create new things out of thin air (actually out of an area of memory called the heap). Up to now we've used pointers solely to point to named variables . So if we used a statement like:

 int data;      int *number_ptr;      number_ptr = &data; 

the thing we are pointing to has a name ( data ). The operator new creates a new, unnamed variable and returns a pointer to it. The "things" created by new can only be referenced through pointers, never by name.

In the following example, we use new to allocate an integer from the heap. The variable element_ptr will point to our new integer.

 int *element_ptr;             // Pointer to an integer element_ptr = new int;        // Get an integer from the heap 

The operator new takes a single argument: the type of the item to be allocated. According to the C++ standard, if new runs out of memory, it throws an exception that normally aborts the program. (See Chapter 22 for information on how to handle this and avoid aborting.) On older C++ systems, when new runs out of memory, it returns a null pointer.

Suppose we are working on a complex record list that contains (among other things) a mailing list. We want to keep our storage use to a minimum, so we only want to allocate memory for a person if he or she exists. Creating an array of class person would allocate the data statically and use up too much space. So we will allocate space as needed. Our structure for a person is:

 class person {      public:         std::string  name;          // Name of the person         std::string  address;       // Where he lives         std::string  city_state_zip;// Part 2 of address         int     age;                // His age         float   height;             // His height in inches } 

We want to allocate space for this person. Later the pointer to this record will be put in the record list.

To create a new person, we use the following:

 struct person *new_ptr;  new_ptr = new person; 

The operator new can also allocate more complex data types such as arrays. Example 20-1 allocates storage for a integer array 80 elements long. The variable data_ptr points to this storage.

Example 20-1. new_array/new_array.cpp
 int main(  )  {      int *data_ptr;      data_ptr = new int[80]; 

All we've done is substitute a simple type (such as person ) with an array specification ( int[80] ).

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net