Yes, C does not prevent out of bound accesses, it specifies them as having undefined behavior.
Undefined behavior can sometimes be very confusing as the program seems to have the expected behavior.
You are modifying a block of memory beyond the end of box
, which could have caused a crash, but did not (or not yet...) and reading back from it returns the written value (which again is undefined behavior, but can seem to work).
The program exits, and there is a chance that no further damage has occurred, but who knows... Modifying box[3][3]
could have altered the place on the stack where the return value of the function is stored, causing a crash after main
returns to its caller, or anything alse... Just hopefully not Monday's fire at Notre Dame.