This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: char* to string: Differences between v4.4.5 and v4.6.1
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Alexander Striffeler <a dot striffeler at students dot unibe dot ch>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 21 Mar 2013 18:18:04 +0000
- Subject: Re: char* to string: Differences between v4.4.5 and v4.6.1
- References: <514B3081 dot 3060801 at students dot unibe dot ch>
On 21 March 2013 16:08, Alexander Striffeler wrote:
> 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?
I think what gets printed to the console depends on your terminal
settings, it's nothing to do with GCC or changes between versions.
The code above will succesfully create a std::string of length six.
Whether you can print NUL bytes to the terminal is nothing to do with
GCC or std::string.