[ub] Type punning to avoid copying

Gabriel Dos Reis gdr at axiomatics.org
Mon Sep 9 06:48:46 CEST 2013


Richard Smith <richardsmith at google.com> writes:

[...]

|     | }
|     |
|     | Now, [basic.life]p1 says that, unless the object has non-trivial
|     | initialization, its lifetime begins "when storage with the proper
|     | alignment and size for type T is obtained". The wording here is
|     | circular, because we don't know whether an object is being initialized
|     | until we know whether its lifetime begins, and vice versa, but it can
|     | be argued that the lifetime of a T object began *before* the data was
|     | copied into the buffer, because storage with suitable size and
|     | alignment was obtained before that point.
| 
|     I don't see the circularity.
| 
|     |
|     | More generally, my view of how the lifetime rules in [basic.life]p1
|     | are intended to work is:
|     |  * If there exists a set of times when objects' lifetimes begin and
|     | end, and that set gives the program defined behavior, then the program
|     | has defined behavior
|     |  * Otherwise, the program has undefined behavior
| 
|     That view may be a novel interpretation of the standards, but I can't
|     see the paragraph you quote supporting that, nor any other disposition,
|     nor does it correspond to any existing interpretation I am aware of.
| 
| 
| The standard says that the lifetime of an object begins "when storage with the
| proper alignment and size for type T is obtained". It says little about what
| constitutes obtaining storage ([expr.new] implies that calling an allocation
| function qualifies, and [basic.align]p8 suggests non-normatively that an object
| of type std::aligned_storage<T>::type qualifies). If a program takes an
| existing buffer and selects a subsequence of characters with the proper size
| and alignment (for instance, using std::align), that seems to qualify, and in
| practice it is very widely assumed that this does qualify as obtaining such
| storage.
| 
| Since the programmer is not required to make an explicit statement of the
| intent to start the lifetime of an object, the compiler cannot declare a
| program to have undefined behavior if such a statement of intent would give it
| defined behavior. 

But the standard does not require an implementation to read the mind of the
programmer.   The implementation can only interpret a program, not what
the programmer intended but didn't write.

| Therefore we can conclude that the *existence* of such intent
| is sufficient to force the implementation to provide the desired behavior.

But where is the material existence of such intent?  Just that "the
programmer might have thought" isn't actionable or executable.

|     In fact, it is not at clear that either programmers or compilers want
|     a notion of 'object resuscitation'.
| 
| 
| I don't know what you mean by 'resuscitation' here. Can you elaborate?

Resuscitation here refer to the idea that 

  "there exists a set of times when objects' lifetimes begin and end, and
   that set gives the program defined behavior, then the program has
   defined behavior"

If we don't have a uniquely defined point in time where the lifetime of
an object starts, that means that either it never started or it started
and ended multitple times.

-- Gaby




More information about the ub mailing list