Topic: Reusing storage, lifetime and memcpy/memset


Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Tue, 6 Feb 2007 15:44:51 GMT
Raw View
Hi,

3.8/1 says:

"The lifetime of an object of type T ends when:
[...]
=FF=FF=FF the storage which the object occupies is reused or released."

What is the meaning of "reusing the storage" here?
Later in the same chapter the word "reuse storage" is used only with=FF
regard to placement new - when placement new is called with the address=FF
of an already existing object, that object ends its lifetime and new=FF
object is created on its place.

What about memcpy? 3.9/2 and 3.9/3 explain the effects of memcpy, but do=FF
not use the word "reuse storage" with respect to this function.

My questions are:

1. Is memcpy *reusing the storage* of the destination object?
2. Is memcpy finishing the lifetime of the destination object?

Somewhat related question:

Is it legal to call memcpy/memset on non-POD destination object if the=FF
subsequent part of the program does not use this object and avoids both=FF
explicit and implicit destruction of this object?
Example:

class NonPOD { /* anything that makes it non-POD */ };

void foo()
{
     NonPOD *np =FF new NonPOD;
     memset(np, 0, sizeof(NonPOD));
}

void bar()
{
     NonPOD np;
     memset(&np, 0, sizeof(NonPOD));
     exit(EXIT_SUCCESS);
}

For the sake of satisfying 3.8/4, let's say that the destructor of=FF
NonPOD has no side-effects on which anybody would depend.

It looks to me that both foo() and bar() are harmless, but I cannot find=FF
any formal provisions for this.

--=FF
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]