Jump to content
  • Advertisement
Sign in to follow this  
kristoferos

std::string bug?

This topic is 4735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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".

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!