Sunday, July 1, 2007

Object Serialization....

AKA - how to do something very useful and possibly shoot yourself very badly in the foot at the same time. Now imagine this scenario:

class Foo
int i;
float fp;

That is serialized as such:
fwrite( &foo, sizeof(Foo), 1, fp);

Then later on in life, reclaimed via:
fread( &foo, sizeof(Foo), 1, fp);

Yes, this is very fast. Yes, it allows for interesting things like mapping variables to memory and so forth. But it really makes life difficult if a couple weeks down the road, after you have serialized objects floating around, you have the need to do something like changing "float fp" to "double fp". Let's not even consider the effects of having a virtual function inside our class...

If there is a need to read a large binary glob from a file, then the glob itself should be saved and read by the class - which would make my life MUCH easier here. Just imagine if I could do something nice and sane like:

Foo bar(stream_source);



Anyway, if you're going to work with a large amount of objects serialized to files, then please please please please PLEASE, consider that someone might need to CHANGE those objects later.


No comments: