Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reinterpret cast to save a struct or class to file

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?

like image 668
IAE Avatar asked Nov 29 '22 17:11

IAE


2 Answers

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, Account is not POD because it has std::string members. The internals of std::string are 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 the std::string object 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::string would 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 Account and expect it to work, because the std::string constructors would not get called.

In short, you cannot use this approach for serializing complex data structures.

like image 180
James McNellis Avatar answered Dec 04 '22 23:12

James McNellis


It's not undefined. Rather, it's platform dependent or implementation defined behavior. This is, in general bad code, because differing versions of the same compiler, or even different switches on the same compiler, can break your save file format.

like image 20
Billy ONeal Avatar answered Dec 04 '22 21:12

Billy ONeal