Sep 3, 2017 at 2:29pm UTC
> It seems that you can create a struct with different variables and then save it to a file (or read it back).
As a sequence of bytes.
Yes, if the struct is a
TriviallyCopyable type.
http://en.cppreference.com/w/cpp/concept/TriviallyCopyable
If not, reading it back as a sequence of (saved) bytes would engender undefined behaviour.
> What does the following do?
*((char *)&storage + t)
Accesses byte number
t in the object representation of
storage .
Last edited on Sep 3, 2017 at 2:32pm UTC
Sep 3, 2017 at 2:40pm UTC
Thanks - so is that last bit pointer arithmetic? Please could you explain what the (char*) does?
Sep 3, 2017 at 2:45pm UTC
That is a C style cast; you take a pointer to something, and then you tell the compiler to treat it as a pointer to something else.
&storage
object of type pointer-to-whatever-kind-of-object-storage-is
(char *)&storage
object of type pointer-to-char, pointing at the object named storage
In C++, a char is of size one byte, it's a kludgy way to access individual bytes of an object.
Sep 3, 2017 at 3:00pm UTC
(char*)&storage : treat the address of storage as a pointer to char (as a pointer to the first byte in the object representation of storage ).
(char*)&storage + t : the address of the byte (char) which is t bytes after the pointer (after the first byte in the object representation)
*((char*)&storage + t) : the value of that particular byte (that particular byte as an lvalue)
Sep 3, 2017 at 3:12pm UTC
thanks both, much appreciated