STL string problems

Started by
17 comments, last by jkleinecke 17 years, 8 months ago
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.
-----------------------------------Indium Studios, Inc.
Advertisement
My guess would be that it's the size of the sting. Is the 4 bytes 12 (or possibly 13) in your example?
Plane9 - Home of the free scene based music visualizer and screensaver
no, the one that I'm looking at is 15 characters. But it doesn't matter, they all seem to be doing that now....
-----------------------------------Indium Studios, Inc.
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.
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 :)
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.
-----------------------------------Indium Studios, Inc.
Try showing a minimal, but complete, code sample that demonstrates your problem. Otherwise all we've got is guesswork to go on.
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?
-----------------------------------Indium Studios, Inc.
What compiler are you using? Your code sample seems to work just fine under MSVC 2003 and gcc 3.4.4.
Visual Studio 2005 Standard
-----------------------------------Indium Studios, Inc.

This topic is closed to new replies.

Advertisement