How to find exactly which vector subscript out of range in Visual Studio

Hello,
Actually I have two questions.

  1. First like in topic: I get error “vector subscript out of range”. It concerns to std::vector. I know what that error means, but the problem is I have a lot of vectors in my app, and I have no idea how to find which one causes the problem. I use Visual Studio, and I am quite new with it, not sure how to debug. I don’t want to spend too much time for understanding VS debuging tools due to fact I usually work in xCode. I use VS just to build my app on Windows.

  2. Second question is at all about differences between Mac OSX and Windows. I use exactly the same code on both systems, on both system I set compiler to C++ 14, and both system set maximum optimalisation for fast code. But on Windows I have some memory leakages like above (vectors out of range). But on Mac there are no problems. Could anyone give some advice or hint?

The workflow is always the same in Windows like in OSX. You need to use the debugger, no way around that. The debugger will exactly set your cursor to the place, where things went wrong. You won’t get a better answer. Finding errors in code without using (and understanding) the debugger, is like finding a sick ant in an anthill…

Same answer like 1: The debugger will tell you in the most convenient way. Nobody will have better answers than the debugger already produces.

Memory leaks are something different than vector subscripts out of bounds: A memory leak is memory, that is not freed when the stack is unrolled at the end of it’s lifetime, vs. vector subscript out of bounds means accessing memory, that is not under your control.

We don’t see your debugger, hence we can’t answer your questions.

There is no way around “spending time to understand the debugger”, sorry…

Hey Daniel,
thanks for your support. OK, now I understand debugger is necessary. But in some way I always find exact problems in xCode. But in VS I just get error that vector is out of range.But could you give some hint how to find which vector is out of range?

like I said:

http://lmgtfy.com/?q=vector+subscript+out+of+range

seriously, bro. you need to learn to self-soothe. read a C++ book and search YouTube for videos of how to use Visual Studio’s debugger.

1 Like

Run your app in debug mode from VS and it will automatically break on the line which contains the vector that caused the error. Use the call stack to move back to the file/function in your project and you should find it easily… just like in Xcode :wink:

I run app in debug mode, and it jump to exact line. But the line is:

_NODISCARD _Ty& operator(const size_type _Pos)
{ // subscript mutable sequence
#if _ITERATOR_DEBUG_LEVEL == 2
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR(“vector subscript out of range”); // IT JUMPS HERE
}
#elif _ITERATOR_DEBUG_LEVEL == 1
_SCL_SECURE_VALIDATE_RANGE(_Pos < size());
#endif /* _ITERATOR_DEBUG_LEVEL */

  return (this->_Myfirst()[_Pos]);
  }

But here I can’t fing anything about vector that it concerns to.

You need to look for the functions leading to that point in the call stack.

Xenakios, thanks :slight_smile:
But I tried on each line of code I showed you “View Call Hierarchy”. Bu t no one line of that code is function. I get message “The current symbol ‘_DEBUG_ERROR’ is not a function.”
How to live :slight_smile:

Hello,
I’ve just found one thing, and I am not sure if it’s OK.
When I set mode to Release )instead of Debug) I get no error, everything works perfect. No problems. Is it OK? Can I leave it like that? Or maybe it’s some danger? If there is no problem in Release mode, then why in Debug mode I get error. Is it very important error that I need to fix it? Or not?

If it has a runtime error when debugging, you can be sure the code is not correct and has to be fixed. An array/vector subscript out of range is a very serious error, even if nothing appears to be wrong when running release builds. It may work for you now, but may fail surprisingly at some other time.

Thanks, so I will try to fix it.emphasized text