【发布时间】:2012-11-24 23:31:56
【问题描述】:
我正在尝试做一些快速而肮脏的矢量序列化,但它没有按预期工作。尝试从文件中读取向量时,问题是段错误。我将文件偏移量和矢量大小存储在标题中。代码如下:
// writing
std::vector<size_t> index;
header.offset = ofs.tellp();
header.size = sizeof(index);
ofs.write((char *) &index[0], sizeof(index)); // pretty bad right, but seems to work
// reading
std::vector<size_t> index;
index.resize(header.numElements)
ifs.seekg(header.offset);
// segfault incoming
ifs.read((char *) &index[0], header.size);
说实话,如果这行得通,我会感到惊讶,但我不确定什么是实现我想要的正确方法。我宁愿远离 boost,但我已经在使用 Qt,所以如果 QVector 或 QByteArray 能以某种方式帮助我,我可以使用它们。
【问题讨论】: