News for hackers. Stuff that matters.
Re:NanoLog – a nanosecond scale logging system for C++ by MauranKilom on Sunday September 01, 2019 @02:56PM (#20854011)
To the best of my knowledge, casting to char* is totally fine (inspecting an object as bytes) under certain constraints.

What is not fine is pretending that an object lives at a position in memory where it does not (i.e. treating bytes of memory as some object through a reinterpret_cast away from char* ).

Edit: To clarify, casting away from char* is of course allowed if you cast to whatever object type actually lives at the pointed-to location.

Re:NanoLog – a nanosecond scale logging system for C++ by userbinator on Sunday September 01, 2019 @07:37PM (#20855676)
(i.e. treating bytes of memory as some object through a reinterpret_cast away from char ).*

That's how people have used C and C++ for decades in low-level work, and it still works exactly as you'd expect, so stop saying "don't do it" because you're only encouraging the compiler-writer-UB-optimisation-nonsense crowd to make things even worse. It's already bad enough that they think the Holy Standard is the only thing that matters.

I believe Linus has several memorable rants on this topic already.

Re:NanoLog – a nanosecond scale logging system for C++ by pingyong on Monday September 02, 2019 @05:07AM (#20857776)
Doesn't matter anymore, Clang and GCC will both make any and all optimizations they can find by assuming you will never hit UB, including just erasing all UB code. You can dislike it, but that's just the reality now, and ignoring it won't make it go away. The only way to change it would be a change in the standard.
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

Re:NanoLog – a nanosecond scale logging system for C++ by Leszek on Sunday September 01, 2019 @03:38PM (#20854295)
I had thought that, as long as the object in that location is valid (e.g. constructed with placement new and not since destructed, with the `char` as a pointer to the storage), then accessing it through the `char` is valid -- but maybe that's only true for `void*` now that I think about it. Either way, fair enough, placement new probably hasn't happened (haven't read the rest of the code).