Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-09 18:52:28


"E. Gladyshev" <egladysh_at_[hidden]> writes:

> You are not answering my question. How does the standard defines
> "reusing storage"?

It does not, just like it doesn't define "allocating storage" or "is"
or "bulwark". OK, "bulwark" isn't in the standard. If the plain
English meaning of "reusing storage" isn't enough for you, a comment
in this example from 3.8/5 should help:

[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
}
---end example]

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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