Re: Rebirthing an object... just making sure
* jkherciueh@gmx.net:
Alf P. Steinbach wrote:
* "Tom????????????????????????????????":
I have an object that represents a listbox on a GUI dialog. In order
to clear the listbox, I can simply delete the listbox and then create
another one. For instance:
// Snippet A
delete p_list;
p_list = new ListBox();
There's the alternative method though that avoids re-allocating memory:
// Snippet B
p_list->~ListBox();
::new(p_list) ListBox();
My question is as follows:
If you're able perform snippet A on a particular object, then are you
ALWAYS able to perform snippet B instead?
I think so, yes, but the two code snippets are not equivalent.
In the first snippet, if ListBox() throws, the listbox object's memory
has been deallocated. In the second snippet, the memory has not been
deallocated. And here there's no easy way to deallocate that memory if
ListBox() throws.
Secondly, I had to check the standard about whether the second snippet
correctly handles a virtual destructor for the case of p_list pointing
to an instance of a derived class. Code that requires standard-checking
is a little ungood for maintainance. And in general, too "clever" code.
If the static and dynamic type of *p_list differ, snippet B has undefined
behavior. Well, more precisely, any code that follows the last line of
snippet B and treats *p_list as a pointer to a valid ListBox object has
undefined behavior.
No, a pointer to an object is a pointer to an object, regardless of what
the object's memory has been used for earlier.
The object's memory (in standardeeze, storage) is just bits.
The reason is the very last item in [3.8/7]:
If, after the lifetime of an object has ended and before the storage which
the object occupied is reused or released, a new object is created at the
storage location which the original object occupied, a pointer that
pointed to the original object, a reference that referred to the original
object, or the name of the original object will automatically refer to the
new object and, once the lifetime of the new object has started, can be
used to manipulate the new object, if:
? the storage for the new object exactly overlays the storage location
which the original object occupied, and
? the new object is of the same type as the original object (ignoring the
top-level cv-qualifiers), and
? the type of the original object is not const-qualified, and, if a class
type, does not contain any non-static data member whose type is
const-qualified or a reference type, and
? the original object was a most derived object (1.8) of type T and the
new object is a most derived object of type T (that is, they are not
base class subobjects).
I think you meant to write "the second item", not "the very last item".
What this list seems to be about is the situation
DerivedListBox* p1 = new DerivedListBox();
ListBox* p2 = p1;
p2->ListBox::~ListBox(); // OK if virtual destructor
::new(p2) ListBox(); // DOES NOT make p1 valid.
Having destroyed the original complete object there's no longer any
DerivedListBox object anywhere. So p1 is now invalid. p2 is good,
because pointers don't care what pointed to bits have been used for
earlier, only what they're used for now... ;-)
On the other hand,
ListBox* p = new DerivedListBox();
p->ListBox::~ListBox(); // OK if virtual destructor
::new(p) ListBox(); // OK (except leakage for exception...)
This last is OK because sizeof(ListBox) <= sizeof(DerivedListBox). But
having done this, knowledge of the amount of memory to deallocate is
lost if it isn't stored elsewhere. The only safe way to delete the
object that I can think of would be to use an explicit destructor call
followed by call to the relevant operator delete with the original size.
I think, the standard was written with a vtable implementation in mind where
the constructors of a class will set the vtaple pointer as their last
operation so that the most derived constructor sets the dynamic type of the
object. If you use placement new on a subobject, you therefore might change
the dynamic type of the ambient object involuntarily.
You might change the dynamic type, yes, but AFAICS vtable implementation
or not has nothing to do with it. The object is represented by its
storage anyway. And that storage is just bits, which retain no memory
of they have been used for.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?