seriously.. I\'m an old hacker from the 80\'s and with some spare time on my hands thought I\'d \'C\' what the fuss is about
( my background is Forth, which does thi
Accessing an array out-of-bounds invokes undefined behaviour. This means that anything could happen, including running without any apparent error.
You miss main return address on the stack and probably overwrite some not critical data of main's caller so your program isn't crash. Moreover on some platforms (like powerpc for example) stack isn't used for function call and return address is stored in the special register (it pushed to the stack when it is required). So it is normal that this incorrect program doesn't crash.
Update: moreover on some system stack grows up (to the higher addresses). At least in ARM stack growth is selectable.
Typically memory is allocated in 4 kB pages. Therefore there is some extra space after the last variable, and your buffer overrun goes undetected. If, however, there was another variable after your members
array and you wrote to members[4]
, that another variable would get corrupted.
Tools like valgrind and dmalloc are often used to detect buffer overruns. They work by allocating special guard regions around your variables and checking that no-one has written there.
C doesn't do any bounds checking. (Forth doesn't either, so I'm not sure where the expectation came from.)
Overflowing an array is undefined behavior: it is allowed, but not required, to crash. In this case, the bytes just happen to be in the same virtual memory page as the stack frame where the local variable was placed. If the frame were towards the end of the page, the CPU would recognize a bad address and complain about the overflow.
If you go a few kilobytes or megabytes out, you will likely see something like you expect.