Hey fam. Still putting my head around array bounds checking
If y'all don't mind what are its disadvantages?
Bounds checking requires the compiler to add extra information to your executable. This could be information like storing the length of the array along with the array during both stack and heap allocations. Also code needs to be added to check the bounds are not exceeded during each array access. Sometimes this is not straightforward with pointers coming into picture which just adds adds more gruesome runtime checks to the mix. If bounds are exceeded then either an exception must be thrown (an expensive operation by itself) or the program must be aborted. All of these impose significant run time impact and were considered not acceptable for a systems programming language when C and C++ were designed. So it is left to the programmers instead.
Having said that, Rust shows us a way to design a system programming language which does bounds checking with very low cost runtime overhead with its fat pointers. But Rust does this because of the way it was designed. Unlike C++, it wasnt designed to be a better C but instead it evolved a new programming paradigm itself. C and C++ unfortunately cant do the same.