Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Segmentation Fault in vector serialize/deserialize

Please help me debug the below code. What I am doing is just serialize vector<string> into binary file and retrieving it back from it. Here is the example main code,

    /*  Portion Commented */
vector<string> list;

list.push_back("AAAAAA");
list.push_back("BBBBBB");
list.push_back("CCCCCC");
list.push_back("DDDDDD");

// Write out a list to a disk file
ofstream os ("/home/test/data.dat", ios::binary);

int size1 = list.size();
os.write((const char*)&size1, sizeof(int));
os.write((const char*)&list[0], size1 * sizeof(string));
os.close();
/* Portion Commented */

// Read it back in
VertexList list2;

ifstream is("/home/test/data.dat", ios::binary);
int size2;
is.read((char*)&size2, sizeof(int));

list2.resize(size2);
cout<<"Size is :"<<size2<<endl;
is.read((char*)&list2[0], size2 * sizeof(string));
for (int i=0; i < size2; i++)
{
        cout<<"At i = "<<i<<", "<<list2[i]<<endl;   //Line 40 in my program
}

I have 4 elements pushed into vector list. Then I serialize the vector and write it in a binary file and retrieving it back from same. It works fine.

Later when I comment the 'Portion Commented' in the above code and tried to retrieve the vector directly from the already created binary file "data.data", it shows segmentation fault event though it prints the size correctly as 4 just before the for loop. This is my valgrind output created with this (valgrind --leak-check=yes ./a.out),

==14058== Invalid read of size 8  
==14058==    at 0x4EBE263: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libstdc++.so.6.0.14)  
==14058==    by 0x40107F: main (test2.cpp:40)  
==14058==  Address 0x2156010 is not stack'd, malloc'd or (recently) free'd 

Line 40 is the cout statement in the last for loop. Can someone help me debug this ? Also tell me whether the above code is portable or not ?

thanks, Prabu

like image 410
Prabu Avatar asked Nov 30 '25 02:11

Prabu


1 Answers

The implementation of std::string includes a pointer to the actual string contents on the heap. So, sizeof(string) is just the pointer plus some bytes. If you want to write the string, you must write the contents itself

for (auto i = list.begin(); i != list.end(); ++i) {
    os.write(i->c_str(), i->size() + 1);
}

When you read that back in, you must look for the terminating NUL byte. Alternatively, you can save the length of the string, as you have done with the list

for (auto i = list.begin(); i != list.end(); ++i) {
    int len = i->size() + 1;
    os.write((const char*)&len, sizeof(len));
    os.write(i->c_str(), i->size() + 1);
}
like image 174
Olaf Dietsche Avatar answered Dec 02 '25 18:12

Olaf Dietsche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!