This preview shows page 1. Sign up to view the full content.
Unformatted text preview: e attempt to dereference a pointer into one of these holes, the operating system will terminate our program with a segmentation exception. Also, some areas of virtual memory are read-only. Attempting to write to one of these areas terminates the program with a protection exception. A common example of dereferencing a bad pointer is the classic scanf bug. Suppose we want to use scanf to read an integer from stdin into a variable. The correct way to do this is to pass scanf a format string and the address of the variable:
scanf("%d", &val) However, it is easy for new C programmers (and experienced ones too!) to pass the contents of val instead of its address:
scanf("%d", val) In this case, scanf will interpret the contents of val as an address and attempt to write a word to that location. In the best case, the program terminates immediately with an exception. In the worst case, the contents of val correspond to some valid read/write area of virtual memory, and we overwrite memory, usually with disastrous and bafﬂing consequences much later. 10.11.2...
View Full Document
- Spring '10
- The American