This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]