1

In my program ] wrongly wrote something on the lines of this:

std::vector<std::string> foo;
std::string test = "hello";
for (int i = 0; i < 10; i++){
    foo.puch_back(test);
}

qDebug() << QString::FromStdString(foo[10]);

This obviously won't work because the index 10 is greater then the size of the vector.

When I run this code on Mac I get "", while when run it on windows I get an error:

index out of bounds

I was wondering why on Windows crashes (like I expected) while on Mac it "works"

Ali
  • 3,373
  • 5
  • 42
  • 54
John Doe
  • 1,613
  • 1
  • 17
  • 35

3 Answers3

1

When accessing memory that you should not access, it can go two ways, depending on whether your program owns that part of memory or not:

  1. That part of memory does not belong to your process: The OS will notice this misbehavior and terminate your program. (The famous segmentation fault)
  2. That part of memory does belong to your process: In that case, you will silently read or overwrite your own data!

Which of these happens may very well be completely random. It depends on the compiler and can also vary with each run, making it a real pain to debug!

The big issue for you as a programmer is the second scenario. The issue can easily go unnoticed but the effects are horrible. The address you are accessing will likely contain data that is part of another variable.

Now if this is just a character or a number, you will probably notice the unusual, seemingly random character/number at the end of your string/vector - but now imagine a vector of objects, e.g. vector<SomeClass> foo You might never bother to look directly at the contents of foo because it doesn't contain nicely human readable information.

The solution, as vivi already hinted at, is to access elements via .at(), because it will always generate an exception when you try to read/write outside the bounds of your array, so your mistake does not go unnoticed and you can fix it right away.

nkaleidoskop
  • 126
  • 4
0

Similar discussion: Vector going out of bounds without giving error

That answer mentioned:

STL vectors perform bounds checking when the .at() member function is called, but do not perform any checks on the [] operator.

When out of bounds, the [] operator produces undefined results.

vivi
  • 334
  • 2
  • 13
  • so windows is just more strict than OS X? since on windows the bounds are checked while on OS X they are not. – John Doe Jun 14 '17 at 15:19
  • Could you share what software do you compile the same code on windows and OS x? and what compilator? it seems there is falgs used by default in some soft, that do this check. here what's discussed: https://stackoverflow.com/questions/1290396/how-to-make-stdvectors-operator-compile-doing-bounds-checking-in-debug-but – vivi Jun 14 '17 at 15:50
  • i use Qt Creator as IDE and compile with gcc on OS X and mingw on Windows – John Doe Jun 15 '17 at 12:02
0

Look at the below documentation:

http://en.cppreference.com/w/cpp/container/vector/operator_at

No bounds check is performed when you use operator [].

So the behavior is undefined and depends on the compiler handling the situation.

But Look into below documentation

http://en.cppreference.com/w/cpp/container/vector/at

std::vector::at This will do bounds checking.

If you try accessing using "at", you will see same result on both.

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • So the difference is that on OS X i compile using gcc while on windows i use mingw and the latter does bound checking with ```operator []``` as well while the first doesn't i guess? – John Doe Jun 14 '17 at 15:18