Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

std::string assignment problem

Status
Not open for further replies.

matman13

Programmer
Feb 18, 2002
12
US
I'm running MSVC++ .NET 2003.

I've got a text file that I need to parse. I'm using std::getline, but I'm running into some problems with missing lines. Here's my code:

std::ifstream infile("myfile.txt");
std::string line;
while (std::getline(infile, line)) {
// Operate on line...
}

I've debugged the std::getline function and found that, under certain conditions (see next paragraph), assignments to the std::string object do not work as expected.

Here is some pretty simple code that reproduces the problem.

std::string s;
s = "abc"; // s is good
s = "abcdefghijklmno"; // s is good
s = "abc"; // s is good
s = "abcdefghijklmnop"; // s is good
s.erase(); // s is BAD
s = "abcdefghijklmnop"; // s is good again
s = "abcdefghijklmnopqrstuvwxyz"; // s is good
s = "abc"; // s is BAD again
s = "abcd"; // s is BAD
s = "abcde"; // s is BAD
s = "abcdefghijklmno"; // s is still BAD
s = "abcdefghijklmnop"; // s is good again
s.clear(); // oops! s is BAD
s = "abc"; // s is BAD
s = "abcdefghijklmnop"; // s is good

The problem seems to occur when transitioning from a string with 16 or more characters to a string with 15 or less characters.

The following link seems to talk about this problem somewhat, but I'm not seeing the assertion. Plus, it's for VC++ 5.0.


Can anyone else reproduce this problem? Is this a known bug? Is there a workaround? I'm simply amazed that this has not been found to date.
 
1. What is it: // s is BAD?
2. Have you #include <string>? std::string? No such member clear()[/i] in std::basic_string.
3. I can't reproduce bad strings in my VC++ 6.0 (w/o clear(), of course).
...
 
When I say "s is BAD," I mean that it contains garbage characters, not the assigned string as one would expect.

Yes, I included <string>. It wouldn't compile otherwise, right? :)

I've got access to a machine that still has VC++ 6.0, and I can't reproduce the problem there either. The clear() method was apparently added in the .NET version.

Again, I'm looking for anyone that can reproduce my problem and knows of a workaround. Thanks!
 
Someone locally gave me some insight. Apparently, std::string has two buffers - one for dynamically-sized strings and one for small strings. This is for optimization purposes.

The .NET debugger shows only one of these buffers; I think it's the dynamic one. So when you go from a 16+ character string to a string with <= 15 characters, the active buffer is changed, but the debugger still shows the other one. Thus, I was seeing the garbage characters in the other buffer and thinking that std::string had the problem.

I found a logic problem that was resulting in the missing lines from parsing my text file. My fault, not std::string. Should've known.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top