Jump to content
  • Advertisement
Sign in to follow this  
jkleinecke

STL string problems

This topic is 4331 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

I've had a strange error crop up recently with STL strings. It seems as though strings are getting 4 bytes of junk data inserted into the beginning of it after passing it as a parameter. For example: Function Prototype: void OpenFile(const std::string& strFile) ; Call: OpenFile("SomeFile.txt") ; After stepping through the debugger I can see that 'strFile' contains a string with contents like this: IIIISomeFile.txt (the 'I's all have a little carat over them). I've narrowed the problem down to the string class because if I change std::string& to char* everything works fine. Has anyone seen this before? I'm 99% certain it's not an ASCII/UNICODE problem but I'm not ruling anything out at this point.

Share this post


Link to post
Share on other sites
Advertisement
My guess would be that it's the size of the sting. Is the 4 bytes 12 (or possibly 13) in your example?

Share this post


Link to post
Share on other sites
How are you using the string? Are you calling s.c_str() to pass it to an fstream object? If not, I'd be inclinded to agree that it is part of the internal representation of the string.

Share this post


Link to post
Share on other sites
Don't worry about that, I'm quite sure it's part of the internal representation, perhaps some uninitialised memory in the block the string is part of? I get that too when stepping through with the debugger, but everything still works fine and to the outside world there's no trace of the oddly accented Is. So, just don't worry, it's not a problem :)

Share this post


Link to post
Share on other sites
I would agree with you except that it crashes while trying to assign one stl string to another stl string. If everything was working the way it was supposed to I wouldn't worry about it, but as it stands I get some major assertions along the way.

Share this post


Link to post
Share on other sites
Try showing a minimal, but complete, code sample that demonstrates your problem. Otherwise all we've got is guesswork to go on.

Share this post


Link to post
Share on other sites
It's a basic memory problem in the string.


std::string g_strSomeString = "" ;

void AssignText(const std::string& strText)
{
g_strSomeString = strText ;
}

int main()
{
AssignText("Foo") ;

return 0 ;
}



After executing the above code you would expect that 'g_strSomeString' would contain the value of "Foo". My problem is that during the execution of the above code, 'strText' in 'AssignText' contains "IIIIFoo" and g_strSomeString contains "" because the data at the beginning of 'strText' is junk data. Any Ideas?

Share this post


Link to post
Share on other sites
What compiler are you using? Your code sample seems to work just fine under MSVC 2003 and gcc 3.4.4.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!