Reading a[3] already causes undefined behavior. Since undefined behavior is never limited locally, it can already lead to the formatting of your hard drive or the computer going to a giant, carnivorous zombie.
In fact, it usually just works. But itβs easy to make a case where the end of the array marks the end of the displayed memory area, so accessing one element outside will lead to segmentation failure. This does not apply to the int array on the stack and with most heap implementations, but you should not rely on it.
(Does the address &a[3] come with undefined behavior.)
source share