简体   繁体   中英

saving data structure c++ to disk

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 ?

boost::serialization could do it. Note: write the archive version first and then do register_types

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM