Valgrind Not Catching Segfaults
c, segmentation-fault, valgrind
Solution
I think you are endowing valgrind with powers rather beyond what it is possible.
It will try to detect various classes of errors and report them to you but it is not possible for it to detect all errors, even in some of the classes of errors it attempts to detect.
In this case what you are dealing with is an out of bounds write to an array which, if valgrind managed to catch it, would be reported as an "invalid write" error. Those are detected by tracking which addresses are "valid" in that they are part of a known heap block.
The problem is that if you index too far past the start or end of an array you may actually wind up with an address that is a valid address in a neighbouring block, which therefore looks absolutely fine to valgrind. To reduce the chance of this happening valgrind adds an area of padding (called a "red zone") each side of a block, but this is only 16 bytes by default.
If you increase the red zone size with the `--redzone-size=128` option then you will find that valgrind does detect the errors in this program.
Problem
I'm aware that Valgrind keeps track of the memory in a way that allows to to catch segfaults. However, why is not catching the following segfault? ``` int main() { char *x = calloc(16, 1); char *y = calloc(16, 1); x[80] = 'c'; y[-80] = 'c'; printf("%c %c\n", *x, *y); return 0; } ``` Isn't it supposed to catch the out of bound access in the heap? According to Valgrind's documentation: ``` But it should detect many errors that could crash your program (eg. cause a segmentation fault). ```