Assigned Reading: 3.6-3.9
Handouts (available on-line): none
Topics Covered:
Some things to note:
In this example, the addresses for the array that A and B points to are very close. See sample run. However, there is a block of memory 16 bytes long that is between these two arrays. If we accidentally overrun the array A with assignments, we can end up overwriting the memory for B and those mysterious 16 bytes.
It turns out that those 16 bytes were used by the memory manager to keep track of the sizes of the blocks of memory that were allocated. Thus, when we delete the array A and the array B, this messes up the internal tables used by the memory manager. The result is a segmentation fault the next time we try to allocate memory.
Recall that the size member is a private member in the Widget class declaration. (See header file.) Nevertheless by type casting a pointer to Widget into a pointer to int we can gain access to both the size and serial members of an object. Thus, you should not think that C++ provides secure protection for private data members.