所以基本上我正在编写一个测试程序,看看我是否可以分别使用 fwrite 和 fread 将队列向量写入和读取到二进制文件中。即使读取和写入部分正确完成并且值正确,我也会收到双重释放或损坏错误。测试代码如下
#include <stdio.h>
#include <vector>
#include <queue>
int main(){
vector<queue<unsigned> > hello(17);
vector<queue<unsigned> > here(17);
queue<unsigned> temp;
for(int f=0;f<4;f++){//initialise a random queue
temp.push(f);
}
for(int i=0;i<hello.size();i++){//assign random queue to every index of vector
hello[i]= temp;
}
FILE *fo;
fo = fopen("hello","wb");
fwrite(&hello[0],sizeof(queue<unsigned>),hello.size(),fo);
fclose(fo);
printf("Writing done!\n");
FILE *fi;
fi=fopen("hello","rb");
fread(&here[0],sizeof(queue<unsigned>),here.size(),fi);
fclose(fi);
printf("Reading done!\n");
for(int i=0;i<here.size();i++){
printf("At key %d value at front is is %d",i,here[i].front());
here[i].pop();
printf(" value %d ",here[i].front());
here[i].pop();
printf(" value %d\n",here[i].front());
}
}
错误似乎是在执行 fread 操作时。