0
    vector <string> inputarrayconverted = { ... };

    try
    {
    if (atoi(inputarrayconverted[p].c_str()) >= 0)
    {
        inputarrayconverted[p] = "n";
    }
}
catch (int e) { }`

is not working. it is giving me a "Debug Assertion Failed!" message. All I'm trying to do is verify that if the array position is an int, change its value to "n"

The error is "Debug Assertion Failed!

File: vector Expression: vector subscript out of range"

However, why is my try block not catching this? That's my question.

This is the stack trace. (I believe)

msvcp100d.dll!590599f3()
[Frames below may be incorrect and/or missing, no symbols loaded for msvcp100d.dll] 
Paradigms Assignment 2 C Plus Plus.exe!std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::operator[](unsigned int _Pos=0)  Line 932 + 0x17 bytes C++
Paradigms Assignment 2 C Plus Plus.exe!wmain(int argc=1, wchar_t * * argv=0x00314af8)  Line 148 + 0x12 bytes    C++
Paradigms Assignment 2 C Plus Plus.exe!__tmainCRTStartup()  Line 552 + 0x19 bytes   C
Paradigms Assignment 2 C Plus Plus.exe!wmainCRTStartup()  Line 371  C
kernel32.dll!7647339a()     
ntdll.dll!77909ef2()    
ntdll.dll!77909ec5()    
Ankit Ahuja
  • 73
  • 2
  • 14
  • why does my compiler (VS2010) give me a "Debug Assertion Failed!"? It seems like perfectly acceptable code... – Ankit Ahuja May 01 '12 at 04:47
  • What does the stack trace look like when you hit the assertion? Which call is asserting? `atoi()`? `c_str()`? array assignment? – user1118321 May 01 '12 at 04:50
  • 1
    I'd also be careful in that according to http://en.cppreference.com/w/cpp/string/byte/atoi `atoi`'s behavior when it's not able to convert the value is to return zero, not negative. It's clunky, but I'd use either sscanf or stringstream to do conversions, as then you can tell if it converted successfully or not. With `atoi` there's no way to tell between a string containing "0" and an error. – Kevin Anderson May 01 '12 at 05:01
  • From *where* are you expecting an `int` to be `throw`n? Also, there are better ways [to convert from a `std::string()` to an `int`](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c). – johnsyweb May 01 '12 at 05:12

2 Answers2

4

The operator[]() access functions on vector do not throw an exception on an out of bounds access - it's simply undefined behavior. The idea being that code that knows it's dealing with index values that are within bounds should not have to pay a penalty for the library to double-check it needlessly.

You're getting a debug assertion because the debug libraries do perform the check, but in the form of the assertion that you see. It would be inappropriate for the debug builds to produce an exception that can be caught in debug builds but not in a release build.

If you want bounds checked access, use vector::at() which performs similar access, but the bounds are checked and an exception will be thrown:

The member function at() provides bounds-checked access to container elements. at() throws out_of_range if n >= a.size().

So, you'll want to catch std::out_of_range instead of an int, and you'll need a #include <stdexcept> to get the declaration for it.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
3

The variable p contains a value that is out of range for the number of elements in the vector. (It's either negative or too big or the array is empty.) Assertions are to tell you something is wrong during development, whereas exceptions handle runtime problems. I don't know Visual Studio very well, so I'm not sure if their version of the standard library has a flag you can set to switch from assertions to exceptions or not. (Maybe just building release does it?)

user1118321
  • 25,567
  • 4
  • 55
  • 86