C++ Logo

std-discussion

Advanced search

Re: About the description of [basic.life]/6

From: Vishal Oza <vickoza_at_[hidden]>
Date: Fri, 10 Apr 2020 10:52:35 -0500
I am not sure if there is undefined behavior the code but if there is any
undefined behavior it the code it is line e. If it work fine the e is a
memory leak as we are using raw pointers. Also the code after f should be
memory leaks as well of p16 and p32_2.

On Fri, Apr 10, 2020 at 9:28 AM merukun1125--- via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> 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.
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2020-04-10 10:57:08