C++ Logo

std-discussion

Advanced search

Re: objects with no lifetime for validation

From: Federico Kircheis <federico_at_[hidden]>
Date: Sat, 27 May 2023 10:16:10 +0200
On 27/05/2023 07.36, Federico Kircheis via Std-Discussion wrote:
> On 26/05/2023 23.23, Jens Maurer wrote:
>>
>> On 26/05/2023 21.30, Federico Kircheis via Std-Discussion wrote:
>>> I know that it is not possible to create objects out of thins air:
>>>
>>> int i = 42;
>>> char buffer[sizeof(int)];
>>> std::memcpy(buffer, &i, sizeof(int));
>>>
>>> int* j = reinterpret_cast<int*>(buffer);
>>> *j; // UB, even if *j == i, as j does not point to an int object
>>
>> This is not accurate since implicit object creation has been introduced.
>> Please read [intro.object] in its entirety.
>
> Mhm, I'll check again...
>


I think you where implying here that memcpy implicitly creates an object
of type int, and thus *j is valid.
Is that correct?

if i was const, would then memcpy create implicitly a const int object?

Received on 2023-05-27 08:16:20