Re:NanoLog – a nanosecond scale logging system for C++
by MauranKilom on Sunday September 01, 2019 @03:26PM
(#20854206)
> it is fine if there actually was an object of that type (or a "similar" type) in that location.
That is correct. See my other nearby reply.
> So casting to char* and back is fine
Indeed.
> casting to char and then to int-type to inspect multiple bytes is fine as long as alignment plays correctly, ...
Only if the place in memory you are pointing to actually has a live int object. In particular, inspecting the byte representation of an object is fine (including copying the bytes elsewhere, which is why they could just use memcpy instead of reinterpret_cast).
See also http://eel.is/c++draft/basic.types#2 and surroundings.
Re:NanoLog – a nanosecond scale logging system for C++
by gpderetta on Sunday September 01, 2019 @03:25PM
(#20854203)
> casting to char and then to int-type to inspect multiple bytes is fine as long as alignment plays correctly
It is only fine if there were the same exact int types at that address. Remember, UB usually isn't the reinterpret_cast but the dereference.
It seems like the function only cast to T* if the input pointer hasn't been changed (the paths that do modify it return early), so there's a value there?