Sign in to follow this  

string not behaving as expected

Recommended Posts

int pos=0;
string tt, buffer="...";
while( pos < buffer.length() ) {
	string tt = buffer.substr(pos, pos+5);
	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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this