eb80
eb80

Reputation: 4738

C++ for loop initializing counter wrong

I have a simple for-loop in C++ and the initialization statement is:

for (int n = 0; n < this->fileLines.size(); n++) {

For some crazy reason, the value of n is being set not to 0 but to 249758, which causes the for-loop to evaluate wrong.

Any ideas why this is initializing wrong (i.e., not to 0)?

enter image description here

Upvotes: 2

Views: 1640

Answers (2)

Richard
Richard

Reputation: 61499

Have you tried sticking

std::cerr<<n<<std::endl;

inside of the for loop? This seems a more direct way of observing the value during the running of the program to verify whether or not it is doing what you think, and optimization will not give you problems with this output.

Perhaps your program is multithreaded and someone is inappropriately writing to that memory location?

Upvotes: 0

jeremy
jeremy

Reputation: 4314

I think you need to verify after the for loop what the value of n is, I don't see any way this could non-0. Check the value at the start of the switch. Your breakpoint may have interrupted before n was actually set.

Upvotes: 6

Related Questions