Error on last value of last box of a vector<int>.

Difficult for me to explain this one.

Have a function that parses a text file. Every time the function finds a match it saves the character number (position in the text file) to a vector<int>.

At the end of the program I print out all the locations of the successful matches between search-term and words-in-file.

The vector<int> I use to store the integer-locations of the foundwords is built using vectorname.push_back(charPosition).

However, on the FINAL box of the vector I get a negative integer value.

All the other boxes in the vector hold the accurate location of which character in the file the word was found on.


The very last vector box contains a negative number (should be character 145, is actually character -111).

Used a cout line to monitor that the values I'm feeding into the vector are accurate. Also used a cout line to monitor that the vector's last box doesn't change when I exit the function and pass the vector back to where it was declared in main();

What am I doing wrong?
Last edited on
What am I doing wrong?

Failing to provide code?
Failing to provide code?


Can you think of anything that would cause a vector<int> to receive incorrect integer values (from an int intname) or return incorrect integer values to where the vector or int was declared in main();?
...to receive incorrect integer values (from an int intname) or ...

I'm not even sure what you mean by this. Can you show us your code? That would make it a lot easier for us to help you debug your problem.

Can you strip down your program to just the bare necessities to compile, run, and demonstrate the problem? That would be the easiest thing for us to debug.

Can you think of anything that would cause a vector<int> to receive incorrect integer values

Certainly. You could put them there. Without seeing code, it's impossible to say if that's what is happening. You are not providing what is necessary to help you.
(should be character 145, is actually character -111).

A positive value suddenly changing to a negative value is indicative of some sort of signed integer overflow. (256-145 = 111). So in some part of your code, it seems like it's trying to fit an int into a signed char, so it overflows. But note that this wrap around from a high positive number to a negative number is technically undefined behavior, and therefore cannot be relied upon. The wraparound, modulus behavior of unsigned ints (max unsigned integer + 1 == 0) can be relied upon, however.

Not sure why you're so resistant to sharing the code; you should be able to simplify your issue down to just a few lines, since you know which input values are causing the problems.
Last edited on
Ganado got the right lead... :)

Turned out I had specified the int that tracked the number of matches as a 'char' by accident in the function definition.

When the 'char' (that was supposed to be an int) had to contain a number greater than 256 that char's maximum of 256 overflowed into negative numbers. So once the char had to handle numbers above 256 it started counting upwards, through negative numbers, towards zero.

+1 Ganado. Saved me having to link code. :)
Topic archived. No new replies allowed.