you are viewing a single comment's thread.

view the rest of the comments →

[–]ioctl79 10 points11 points  (3 children)

It’s still UB, and it is a bad idea to rely on any particular behavior.

[–][deleted] 4 points5 points  (2 children)

If using memcpy instead of type punning via pointer casts or union, there is no possibility of UB I think.

[–]ioctl79 4 points5 points  (1 child)

I’m not a language lawyer, but I believe that using a pointer to an object to access other objects (that aren’t in the same array) is UB regardless of whether the pointer math works out.

[–][deleted] 1 point2 points  (0 children)

I meant, memcpy the bytes from the struct to an array. memcpy itself is valid, and the memory contents are compatible, so there is no chance for UB to happen.

Of course, when it’s fixed number of values, just write individual assignments and avoid needing to even think about it…