More Effective C++ (Shawn Kahl's Library) by Scott Meyers

More Effective C++ (Shawn Kahl's Library) by Scott Meyers

Author:Scott Meyers
Language: eng
Format: epub
Publisher: Addison-Wesley
Published: 1996-04-23T16:00:00+00:00


but the language makes no guarantee that this is how it will be done. Some compilers generate the function calls in this order instead:

Call operator new for first object

Call operator new for second object

Call constructor for second object

Call constructor for first object

There is nothing wrong with compilers that generate this kind of code, but the set-a-bit-in-operator-new trick fails with such compilers. That's because the bit set in steps 1 and 2 is cleared in step 3, thus making the object constructed in step 3 think it's not on the heap, even though it is.

These difficulties don't invalidate the basic idea of having each constructor check to see if *this is on the heap. Rather, they indicate that checking a bit set inside operator new (or operator new[]) is not a reliable way to determine this information. What we need is a better way to figure it out.

If you're desperate enough, you might be tempted to descend into the realm of the unportable. For example, you might decide to take advantage of the fact that on many systems, a program's address space is organized as a linear sequence of addresses, with the program's stack growing down from the top of the address space and the heap rising up from the bottom:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.