0

I'm from Java, and new to c++.

Java can detect OutOfIndex exception at runtime, but in C++, out of index in array will cause unexpected result. Why is that? Will this bring any benefit?

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
  • Java arrays have a fixed length (property). C++ arrays do not. – Elliott Frisch Jun 23 '18 at 13:53
  • Philosophical differences ("you don't pay for what you don't use"). If you want bounds checking, use vectors and `std::vector::at()`. http://www.cplusplus.com/reference/vector/vector/at/ – NPE Jun 23 '18 at 13:53
  • Having just asked [why arrays have no "length attribute"](https://stackoverflow.com/questions/51001777/c-why-array-doesnt-have-length-attribute), you now ask why it doesn't validate an index against the non-existent "length attribute"? – Bo Persson Jun 23 '18 at 13:59
  • If you want range checking for containers (which has a cost), use `.at()` rather than `operator[]`. You get what you ask for.. – Jesper Juhl Jun 23 '18 at 14:03

0 Answers0