I recently came across a rather interesting serialization approach that utilized the transparency (common undefined behavior among compilers?) of uninitialized variables for "efficient" deserialization.
Memory is allocated and assigned a predetermined value. Placement new is then used to instantiate a struct (for example a complex inplace data structure) "initializing" the uninitialized variables to the value of the underlying memory. (see code below)
Besides from being rather risky and possibly not a very agreeable coding convention... I was just wondering whether anyone had come across this method or more importantly -- what is it called?
class SomeClass {
public:
SomeClass() {}
int someInt;
};
int main(...) {
int dummy = 42;
int *pSomeClass = new (&dummy) SomeClass();
cout << pSomeClass->someInt << endl;
}
This will print out the number 42... neato!