0

I was wondering why accessing elements outside of array bound is allowed when it is clearly mentioned as undefined.

pasha
  • 2,035
  • 20
  • 34
  • 6
    Basically because protecting the developer from themselves isn't always free. If you want runtime protection you can ask for it by using `std::vector::at` but you pay the performance hit of having every access checked. – François Andrieux Sep 14 '20 at 14:45
  • so, is it performance related? – pasha Sep 14 '20 at 14:46
  • 3
    Correct, 100% performance related. Performance is one of the touchstones of C++. – Eljay Sep 14 '20 at 14:46
  • 3
    c++ has a principle of not paying for what you don't need. If it checked every time for out of bounds there would be a non zero time cost to do so. – drescherjm Sep 14 '20 at 14:47
  • Note that compiling your code with (the tremendously expensive) `-fsanitize=undefined` or `-fsanitize=address` *will* result in a process-terminating runtime error. – bitmask Sep 14 '20 at 15:05
  • The issue also has to do with code space. Many embedded systems have limited memory area for code. There is a tradeoff on error checking and code space. – Thomas Matthews Sep 14 '20 at 15:16

0 Answers0