Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:14:02+00:00 2026-06-14T10:14:02+00:00

Please help me debug the below code. What I am doing is just serialize

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:14:03+00:00Added an answer on June 14, 2026 at 10:14 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me somebody to convert unicodestring into string This is how i am
Please help me look into my simple test case: import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import
please help me to identify which of these following is more optimized code? for(int
Please help me understand the following code snippet :- def any(l): whether any number
Can someone please help? I use Google code’s Moq framework for mocking within my
I need some help in rendering a dynamic gridview. Using the source code below,
The code block below is not running when executed from a batch file. It
In the sample code below std::string result = exec( dir ) ; cout<<result; I
I want to display some text below each grid image. Please see my code
Please some help on how to debug this! On my development machine I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.