• Advertisement
Sign in to follow this  

string not behaving as expected

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

int pos=0;
string tt, buffer="...";
while( pos < buffer.length() ) {
	string tt = buffer.substr(pos, pos+5);
	tt.clear();
	pos += 5;
}
When I check the length of string 'tt', it keeps getting larger and larger, when I expect the size to always be 5.

Share this post


Link to post
Share on other sites
Advertisement
The second parameter to substr is the length, not the end position.

Share this post


Link to post
Share on other sites
string tt;
while ( ... ) {
string tt = ...;
}


You're defining tt twice. When you call tt.clear(), you're clearing the string that will be discarded at the end of the loop anyways.

For the GCC compiler, if you want to recieve warnings about this, you can use -Wshadow. Don't know about Microsoft's compilers, sorry.

EDIT: err, substr is the reason it's growing (you must be inspecting it's size, as the outer tt is never modified), but this is still probably a problem IMO.

Share this post


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

  • Advertisement