Serialization of complex structures in C++

前端 未结 2 1003
青春惊慌失措
青春惊慌失措 2020-12-17 00:14

I\'m trying to serialize a set of structs in C++. This works great for all data except a vector contained in my struct. I can write the data to disk, and then read all data

2条回答
  •  失恋的感觉
    2020-12-17 00:42

    What you are doing is taking copying a contiguous region of memory in which you have stored one and wrote it to disk. This will work fine for simple data types (POD in C++ jargan). The problem with vectors is that a vector is a complex object with pointers to other regions of memory. These other regions of memory don't exist when you de-serialize one into two hence your segmentation fault.

    Unfortunately there is no shortcut, some form on custom serialization code will need to be written to get the job done.

    As some have already mentioned Boost Serialization may help. Alternatively roll out your own.

提交回复
热议问题