std::string bug?

Started by
20 comments, last by MaulingMonkey 18 years, 8 months ago
Hello, is it possible to have a string containing the '\x00' character without it thinking that its the end of the string. For example, I want to have some data after the '\x00' character such as "\xFF\xFF\xFF\xFF\x00\xFF".
Advertisement
You could simply have another character acting as the \0.

ace
What problems did you encounter? Using G++, I can put null characters in my strings without any effort.

Quote:MSDN page:
Objects of type string belonging to the C++ template class basic_string<char> are not necessarily null terminated. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string but has not special meaning in an object of type string and may be a part of the string just like any other character.
a stl string does not have to be terminated by the \x00 character, but then you do actually have to tell it what size it should be.
String processing functions are mostly by nature null terminated, so erm I suppose it really depends on what you are planning to do with that string.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote:Original post by paulecoyote
a stl string does not have to be terminated by the \x00 character, but then you do actually have to tell it what size it should be..


std::string foo = "foo";foo[1] = '\0';


Now foo is the string made of the characters f, \0, o (in that order).
And if it does give you trouble there's a nift container std::vector that would work just fine without any second thoughts about it.

Oh, and null terminated strings really are crap for many many things, when doing heavy stringwork they simply are among the worst there is.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
The problem is most likely that you tried to assign it from a string literal. You can't stick \0 in a string literal; the assignment will think it's the end of the string. If you do mystring = std::string(string_with_nulls,length) instead of mystring = string_with_nulls it will work fine. If you have nulls in the string you also can't use the c_str member function.
Thanks for all of your replies, passing the length as an argument worked but how would I be able to get the length of the big strings that change all the time?
Just call the length() method.
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
Quote:Original post by ToohrVyk
Quote:Original post by paulecoyote
a stl string does not have to be terminated by the \x00 character, but then you do actually have to tell it what size it should be..


std::string foo = "foo";foo[1] = '\0';


Now foo is the string made of the characters f, \0, o (in that order).


I meant if you used strcpy or something like that that uses null terminating characters as a stop. What you've done there is declare something with 3 elements and change the 2nd one, so that is bound to work.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

This topic is closed to new replies.

Advertisement