Chapter 20. Advanced Pointers

A race that binds Its body in chains and calls them Liberty, And calls each fresh link progress.

—Robert Buchanan

One of the more useful and complex features of C++ is its use of pointers. With pointers you can create complex data structures such as linked lists and trees. Figure 20-1 illustrates some of these data structures.

Examples of pointer use

Figure 20-1. Examples of pointer use

Up to now all your data structures have been allocated by the compiler as either permanent or temporary variables. With pointers you can create and allocate dynamic data structures, which can grow or shrink as needed. In this chapter you will learn how to use some of the more common dynamic data structures.

It should be noted that C++ has something called the Standard Template Library (STL), which implements the data structures presented in this chapter. These STL classes are much more reliable and flexible when it comes to managing dynamic memory. Thus, in practice, people rarely explicitly use the data structures described below.

We will study them anyway, however, because by examining them we’ll learn a lot about the basics of dynamic memory management. The linked list and tree are classic dynamic memory management tools and understanding them will give you the skills to create more complex, real world, data structures.

Pointers, Structures, and Classes

Structures and classes may contain ...

Get Practical C++ Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.