char* to string: Differences between v4.4.5 and v4.6.1

Alexander Striffeler a.striffeler@students.unibe.ch
Thu Mar 21 16:08:00 GMT 2013


Hello

I'm using g++ for a project where I have to store serialized data as a 
char*. Of course, these serialized bits are likely to contain NULL 
characters which implies that NULL-terminating strings obviously are a 
bad choice. Unfortunately, storing the information as char* is 
predetermined. This means I serialize the objects, process them as 
std::string and then store them as char*. When I try to recover the 
std::string, the behaviour is kind of strange (consider the code snippet 
below as an example):

         const char* hello = "hel\0lo";
         std::string s(hello, 6);
         std::cout << "s.length() = " << s.length() << '\n';
         std::cout << "s = \"" << s << "\"\n";

prints 'hel' on my machine running g++ v4.6.1 (which apt pretends to be 
up to date) while it prints 'hello' on a stackoverflow contributor's 
machine running version 4.4.5. (For the full question see 
stackoverflow.com/questions/15525208).

Is this a known issue - and does anyone know a workaround?

Any hints are very appreciated - TIA!

Cheers
Alex



More information about the Gcc-help mailing list