Online Book Reader

Home Category

Professional C__ - Marc Gregoire [369]

By Root 1056 0
are just for illustrative purposes. Addresses on a real system are highly dependent on your hardware and operating system.

Readers who are more comfortable with spatial representations might derive more benefit from the “arrow” view of pointers. A pointer is simply a level of indirection that says to the program “Hey! Look over there.” With this view, multiple levels of pointers simply become individual steps on the path to data. Figure 21-11 showed a graphical view of pointers in memory.

When you dereference a pointer, by using the * operator, you are telling the program to look one level deeper in memory. In the address-based view, think of a dereference as a jump in memory to the address indicated by the pointer. With the graphical view, every dereference corresponds to following an arrow from its base to its head.

When you take the address of a location, using the & operator, you are adding a level of indirection in memory. In the address-based view, the program is simply noting the numerical address of the location, which can be stored as a pointer. In the graphical view, the & operator creates a new arrow whose head ends at the location designated by the expression. The base of the arrow can be stored as a pointer.

Casting with Pointers

Since pointers are just memory addresses (or arrows to somewhere), they are somewhat weakly typed. A pointer to an XML Document is the same size as a pointer to an integer. The compiler will let you easily cast any pointer type to any other pointer type using a C-style cast:

Document* documentPtr = getDocument();

char* myCharPtr = (char*)documentPtr;

A static cast offers a bit more safety. The compiler will refuse to perform a static cast on pointers to different data types:

Document* documentPtr = getDocument();

char* myCharPtr = static_cast(documentPtr); // BUG! Won't compile

If the two pointers you are casting are actually pointing to objects that are related through inheritance, the compiler will permit a static cast. However, a dynamic cast is a safer way to accomplish a cast within an inheritance hierarchy. Consult Chapter 8 for details on dynamic casts.

const with Pointers

The interaction between the const keyword and pointers is a bit confusing because it is unclear to what you are applying const. If you dynamically allocate an array of integers and apply const to it, is the array address protected with const, or are the individual values protected? The answer depends on the syntax.

If const occurs before the type, it means that the pointed-to value is protected. In the case of an array, the individual elements of the array are const. The following function receives a pointer to a const integer. The first line will not compile because the actual value is protected by const. The second line would compile, because the pointer itself is unprotected:

void test(const int* inProtectedInt, int* anotherPtr)

{

*inProtectedInt = 7; // BUG! Attempts to write to const value

inProtectedInt = anotherPtr; // Works fine

}

To protect the pointer itself, the const keyword immediately precedes the variable name, as shown in the following code. This time, both the pointer and the pointed-to value are protected, so neither line would compile:

void test(const int* const inProtectedInt, int* anotherPtr)

{

*inProtectedInt = 7; // BUG! Attempts to write to const value

inProtectedInt = anotherPtr; // BUG! Attempts to write to const value

}

In practice, protecting the pointer is rarely necessary. If a function is able to change the value of a pointer that you pass it, it makes little difference. The effect will only be local to the function, and the pointer will still point to its original address as far as the caller is concerned. Marking a pointer as const is more useful in documenting its purpose than for any actual protection. Protecting the pointed-to value(s), however, is quite common to protect against overwriting shared data, and to allow the compiler to perform more powerful optimizations.

ARRAY-POINTER DUALITY


You have already seen some of the overlap

Return Main Page Previous Page Next Page

®Online Book Reader