I have a very complex data structure with pointers to various parts of the memory. It takes a while to build and I would like to save the "memory" used by that structure to the disk. Then, when the program is launched again, it would just memory map the file and I could use it. Is there any way to do this ?
saving data structure c++ to disk
2.8k views Asked by madreblu AtThere are 3 answers
It can be done, yes, but you need to be aware of how the structure is actually stored in memory, and how that changes on various platforms (alignment, packing, byte-ordering, primitive type sizes, etc.). Additionally, it would be prudent to change your "pointers to various parts" into "offsets to various parts" since actual memory locations will change between process uses.
example, writing an object like this to disk should be fairly portable:
#pragma pack(1)
typedef struct {
unsigned char data8[8];
unsigned char data4[4];
unsigned char offset[2];
unsigned char data2[2];
} MyStruct;
#pragma pack() // to return to default
but one like this would be very problematic:
typedef struct _MyStruct {
unsigned long long data8;
unsigned int data4;
struct _MyStruct *nextOne;
unsigned short data2;
} MyStruct;
You could start with Boost.Interprocess and use memory maped files. Your data structure will look a litle differnt (pointers are smart pointers, references forbidden, no virtual functions). But it should be manageable. At least all types you use needs to be constructable in shared memory. Some are (Boost.Container) and some are not (Boost.Any, Boost.Variant).
boost::serialization could do it. Note: write the archive version first and then do register_types