2 ms·
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, c
by detaro 7y ago
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?
- gpderetta 7y ago> 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.
- MauranKilom 7y ago> 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 http://eel.is/c++draft/basic.types#2 and surroundings.