Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-10-11 13:21:29


--- Bo Persson <bop2_at_[hidden]> wrote:
[...]
>
> You are making it all too difficult for you! :-)

I am actually having a lot fun. :)

[...]
>
> On some systems p2 will now point at the same space that p1 formerly
> used. But p1 is invalid and p2 is valid. This is about what you did,
> even though you did in the most difficult way.

I did it almost the same as in the following example in the standard.

[Example:
struct B {
  virtual void f();
  void mutate();
  virtual ~B();
};
struct D1 : B { void f(); };
struct D2 : B { void f(); };
void B::mutate() {
  new (this) D2; // reuses storage -- ends the lifetime of *this
  f(); // undefined behavior
  ... = this; // OK, this points to valid memory
}
void g() {
  void* p = malloc(sizeof(D1) + sizeof(D2));
  B* pb = new (p) D1;
  pb->mutate();
  &pb; // OK: pb points to valid memory
  void* q = pb; // OK: pb points to valid memory
  pb->f(); // undefined behavior, lifetime of *pb has ended
}

In general placement 'new' opened a can or worms.
On one hand the standard doesn't want you
to mess around with object allocations
on the other hand if you really want it,
there is this placement 'new' but
not so fast. If you *really* try to manage
the storage, you are ending object lifetimes.

Eugene

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk