So basically I was writing a test program to see if I could write and read a vector of queues into a binary file using fwrite and fread respectively. Even though the reading and writing part is done correctly and the values are correct, I get a double free or corruption error. The test code is the following
#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());
}
}
The error seems to be when doing the fread operation.