Access Violations
We first discussed segmentation faults in the chapter on indirect access, but we always need to keep this in mind when we are working with pointers. You are likely to encounter more of these kinds of issues once you start dealing with memory management, as there will be more opportunities for these issues to occur.
Access violations occur when you try to access memory in a way that isn’t permitted by memory protection in the operating system. This will cause your program to crash. Figure x.y shows a common example where this occurs. Trying to follow a pointer to nothing (address 0, which uses the symbol NULL
in C/C++) will crash the program with an access violation. This applies whether you are reading or writing to the value at that address. The common name for this kind of error is a segmentation fault, segfault for short.
The only way to avoid these access violations is to take care with your pointers, see Figure x.y. When you start working with pointers you need to go a little slower, and think a little more carefully about what it is you are doing. Having a good understanding of how these dynamic memory allocation tools work is the first step toward achieving this.