Date: Fri, 10 Apr 2020 23:28:32 +0900 (JST)
Inspired by: https://teratail.com/questions/250362 and
https://stackoverflow.com/questions/56977042/reusing-objects-space-by-another-object.
At the beginning of [basic.life]/6
<https://timsong-cpp.github.io/cppwp/n4659/basic.life#6>, the following was written:
Before the lifetime of an object has started but after the storage which
the object will occupy has been allocated or, after the lifetime of
an object has ended and before the storage which the object occupied is reused or
released, any pointer that represents the address of the storage location where the
object will be or was located may be used but only in limited ways.
However, the example in [basic.life]/6 also describes a pointer to the
storage which the new object was created.
Does the description of [basic.life]/6, especially the description after the
"otherwise" clause, also say a pointer to the storage which another new object was created?
Furthermore, does (a) to (f) in the following code cause undefined behavior?
#include <cstdint>
#include <new>
int main()
{
std::uint32_t* p32 = new std::uint32_t[4]{};
std::uint16_t* p16 = new (p32) std::uint16_t[4]{};
*p32; // (a) (indirect references of type void* are not allowed)
std::uint32_t lval = *p32 + 100; // (b)
std::uint32_t* p32_2 = p32 + 2; // (c)
*p32_2 = 0; // (d)
p32 = new (p32) std::uint32_t[4]; // (e)
delete [] p32; // (f)
}
Thank you.
https://stackoverflow.com/questions/56977042/reusing-objects-space-by-another-object.
At the beginning of [basic.life]/6
<https://timsong-cpp.github.io/cppwp/n4659/basic.life#6>, the following was written:
Before the lifetime of an object has started but after the storage which
the object will occupy has been allocated or, after the lifetime of
an object has ended and before the storage which the object occupied is reused or
released, any pointer that represents the address of the storage location where the
object will be or was located may be used but only in limited ways.
However, the example in [basic.life]/6 also describes a pointer to the
storage which the new object was created.
Does the description of [basic.life]/6, especially the description after the
"otherwise" clause, also say a pointer to the storage which another new object was created?
Furthermore, does (a) to (f) in the following code cause undefined behavior?
#include <cstdint>
#include <new>
int main()
{
std::uint32_t* p32 = new std::uint32_t[4]{};
std::uint16_t* p16 = new (p32) std::uint16_t[4]{};
*p32; // (a) (indirect references of type void* are not allowed)
std::uint32_t lval = *p32 + 100; // (b)
std::uint32_t* p32_2 = p32 + 2; // (c)
*p32_2 = 0; // (d)
p32 = new (p32) std::uint32_t[4]; // (e)
delete [] p32; // (f)
}
Thank you.
Received on 2020-04-10 09:31:27