A segmentation error occurs if you try to access virtual address data that is not tied to your process. On most operating systems, memory is displayed on pages of several kilobytes; this means that you often will not get an error if you write off the end of the array, because there are other reliable data on the memory page.
A bus error indicates a lower level error; as you say, there are two reasons: incorrect access or missing physical address. However, the first of these does not occur here, since you are dealing with bytes that have no alignment restrictions; and I think the second can only happen when accessing data, when the memory is completely exhausted, which probably does not happen.
However, I think you can also get a bus error if you try to execute code from an invalid virtual address. This may be what happens here - by writing off the end of the local array, you will overwrite important parts of the stack frame, such as the function return address. This will cause the function to return to the wrong address, which (I think) will give a bus error. This is my best guess about what a special undefined taste you have here.
In general, you cannot rely on segmentation errors to intercept buffers; The best tool I know is valgrind , although it still won't be able to catch some kind of overspending. The best way to avoid overflow when working with strings is to use std::string , rather than pretending that you are writing C.
source share