The new and delete operators provide a more flexible approach than automatic and static variables. They manage a pool of memory, which C++ refers to as the
Stacks, Heaps, and Memory Leaks
What happens if you
Even the best programmers and software companies create memory leaks. To avoid them, it’s best to get into the habit of joining your new and delete operators immediately, planning for and entering the deletion of your construct as soon as you dynamically allocate it on the free store. C++’s smart pointers (Chapter 16) help automate the task.
Note
Pointers are among the most powerful of C++ tools. They are also the most dangerous because they permit computer-unfriendly actions, such as using an uninitialized pointer to access memory or attempting to free the same memory block twice. Furthermore, until you get used to pointer notation and pointer concepts through practice, pointers can be confusing. Because pointers are an important part of C++ programming, they weave in and out of future discussions in this book. This book discusses pointers several more times. The hope is that each exposure will make you more comfortable with them.
Combinations of Types
This chapter has introduced arrays, structures, and pointers. These can be combined in various ways, so let’s review some of the possibilities, starting with a structure:
struct antarctica_years_end
{
int year;
/* some really interesting data, etc. */
};
We can create variables of this type:
antarctica_years_end s01, s02, s03; // s01, s02, s03 are structures
We can then access members using the membership operator:
s01.year = 1998;
We can create a pointer to such a structure:
antarctica_years_end * pa = &s02
Provided the pointer has been set to a valid address, we then can use the indirect membership operator to access members:
pa->year = 1999;
We can create arrays of structures:
antarctica_years_end trio[3]; // array of 3 structures
We then can use the membership operator to access members of an element:
trio[0].year = 2003; // trio[0] is a structure
Here, trio is an array, but trio[0] is a structure, and trio[0].year is a member of that structure. Because an array name is a pointer, we also can use the indirect membership operator:
(trio+1)->year = 2004; // same as trio[1].year = 2004;
We can create an array of pointers:
const antarctica_years_end * arp[3] = {&s01, &s02, &s03};
This is starting to look a bit complicated. How can we access data with this array? Well, if arp is an array of pointers, then arp[1] must be a pointer, and we can use the indirect membership operator with it to access a member:
std::cout << arp[1]->year << std::endl;
We can create a pointer to such an array:
const antarctica_years_end ** ppa = arp;