News for hackers. Stuff that matters.
Re:NanoLog – a nanosecond scale logging system for C++ by detaro on Sunday September 01, 2019 @03:19PM (#20854165)
If I remember correctly, it is fine if there actually was an object of that type (or a "similar" type) in that location. So casting to char* and back is fine, casting to char and then to int-type to inspect multiple bytes is fine as long as alignment plays correctly, ...

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?

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.

Re:NanoLog – a nanosecond scale logging system for C++ by detaro on Sunday September 01, 2019 @03:27PM (#20854222)
I thought the int-types had a special exception making that always possible (if there's a bunch of bytes, you can turn them into an int), but I could very well be wrong on that detail.
Re:NanoLog – a nanosecond scale logging system for C++ by gpderetta on Sunday September 01, 2019 @03:50PM (#20854398)
Char is the only exception.

And now std::byte IIRC