Sign in to follow this  

binary and classes?

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to save and load class's settings into/from a binary file? Using the same method as for structs seems to fail miserably and the program comes crashing down from the skies. // edit: c++, i'm talking about

Share this post


Link to post
Share on other sites
Quote:
Original post by Paul Richardson
Remember the first u32 of a class is generally the vtable pointer, to you need to take that into account...


Only when it's a derived class, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Quote:
Original post by Paul Richardson
Remember the first u32 of a class is generally the vtable pointer, to you need to take that into account...


Only when it's a derived class, right?


Not particularly. Only when it has any virtual functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paul Richardson
Remember the first u32 of a class is generally the vtable pointer, to you need to take that into account...


Is the 'memory layout' of a class defined? Could you serialize a class based on that, like with structs?

Share this post


Link to post
Share on other sites
No, don't rely the location (or number of) vtable pointers; it is implementation-defined and so could be different from compiler to compiler.

You generally don't want to simply "serialize" a class or struct by doing something like write(&foo,sizeof(foo)) where foo is an instance of some class. There are a number of problems with this.

-: The vtable(s) are in there somewhere (in C++, they could exist regardless of whether or not you have a struct or class).
-: There is likely padding between data members that senselessly increase the size of the file on disk and also make it more cumbersome to read back.
-: There could be pointers in the class, which don't serialize properly.

Share this post


Link to post
Share on other sites

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this