This is something the professor showed us in his scripts. I have not used this method in any code I have written.
Basically, we take a class, or struct, and reinterpret_cast it and save off the entire struct like so:
struct Account
{
Account()
{ }
Account(std::string one, std::string two)
: login_(one), pass_(two)
{ }
private:
std::string login_;
std::string pass_;
};
int main()
{
Account *acc = new Account("Christian", "abc123");
std::ofstream out("File.txt", std::ios::binary);
out.write(reinterpret_cast<char*>(acc), sizeof(Account));
out.close();
This produces the output (in the file)
ÍÍÍÍChristian ÍÍÍÍÍÍ ÍÍÍÍabc123 ÍÍÍÍÍÍÍÍÍ
I’m confused. Does this method actually work, or does it cause UB because magical things happen within classes and structs that are at the whims of individual compilers?
It doesn’t actually work, but it also does not cause undefined behavior.
In C++ it is legal to reinterpret any object as an array of
char, so there is no undefined behavior here.The results, however, are usually only usable if the class is POD (effectively, if the class is a simple C-style struct) and self-contained (that is, the struct doesn’t have pointer data members).
Here,
Accountis not POD because it hasstd::stringmembers. The internals ofstd::stringare implementation-defined, but it is not POD and it usually has pointers that refer to some heap-allocated block where the actual string is stored (in your specific example, the implementation is using a small-string optimization where the value of the string is stored in thestd::stringobject itself).There are a few issues:
You aren’t always going to get the results you expect. If you had a longer string, the
std::stringwould use a buffer allocated on the heap to store the string and so you will end up just serializing the pointer, not the pointed-to string.You can’t actually use the data you’ve serialized here. You can’t just reinterpret the data as an
Accountand expect it to work, because thestd::stringconstructors would not get called.In short, you cannot use this approach for serializing complex data structures.