Exceeding array bound in C — Why does this NOT crash?

C compilers generally do not generate code to check array bounds, for the sake of efficiency. Out-of-bounds array accesses result in “undefined behavior”, and one
possible outcome is that “it works”. It’s not guaranteed to cause a crash or other
diagnostic, but if you’re on an operating system with virtual memory support, and your array index points to a virtual memory location that hasn’t yet been mapped to physical memory, your program is more likely to crash.

Leave a Comment