keypad
Writing to read-only memory Writing to read-only memory raises a segmentation fault. At the level of code errors, this occurs when the program writes to part of its own
code segment or the read-only portion of the
data segment, as these are loaded by the OS into read-only memory. Here is an example of
ANSI C code that will generally cause a segmentation fault on platforms with memory protection. It attempts to modify a
string literal, which is undefined behavior according to the ANSI C standard. Most
compilers will not catch this at compile time, and instead compile this to executable code that will crash: int main(void) { char* s = "hello world"; *s = 'H'; } When the program containing this code is compiled, the string is placed in the
rodata section of the program
executable file: the read-only section of the
data segment. When loaded, the operating system places it with other strings and
constant data in a read-only segment of memory. When executed, a variable, s, is set to point to the string's location, and an attempt is made to write an character through the variable into the memory, causing a segmentation fault. Compiling such a program with a compiler that does not check for the assignment of read-only locations at compile time, and running it on a Unix-like operating system produces the following
runtime error: $ gcc segfault.c -g -o segfault $ ./segfault Segmentation fault
Backtrace of the core file from
GDB: Program received signal SIGSEGV, Segmentation fault. 0x1c0005c2 in main () at segfault.c:6 6 *s = 'H'; This code can be corrected by using an array instead of a character pointer, as this allocates memory on stack and initializes it to the value of the string literal: char s[] = "hello world"; s[0] = 'H'; // equivalently, *s = 'H'; Even though string literals should not be modified (this has undefined behavior in the C standard), in C they are of static char[] type, so there is no implicit conversion in the original code (which points a char* at that array), while in C++ they are of static const char[] type, and thus there is an implicit conversion, so compilers will generally catch this particular error.
Null pointer dereference In C and C-like languages,
null pointers are used to mean "pointer to no object" and as an error indicator, and
dereferencing a null pointer (a read or write through a null pointer) is a very common program error. The C standard does not say that the null pointer is the same as the pointer to
memory address 0, though that may be the case in practice. Most operating systems map the null pointer's address such that accessing it causes a segmentation fault. This behavior is not guaranteed by the C standard. Dereferencing a null pointer is
undefined behavior in C, and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null. int* ptr = NULL; printf("%d", *ptr); This sample code creates a
null pointer, and then tries to access its value (read the value). Doing so causes a segmentation fault at runtime on many operating systems. Dereferencing a null pointer and then assigning to it (writing a value to a non-existent target) also usually causes a segmentation fault: int* ptr = NULL; • ptr = 1; The following code includes a null pointer dereference, but when compiled will often not result in a segmentation fault, as the value is unused and thus the dereference will often be optimized away by
dead code elimination: int* ptr = NULL; • ptr;
Buffer overflow The following code accesses the character array s beyond its upper boundary. Depending on the compiler and the processor, this may result in a segmentation fault. char s[] = "hello world"; char c = s[20];
Stack overflow Another example is
recursion without a base case: int main(void) { return main(); } This code causes the
stack to overflow, which results in a segmentation fault. Infinite recursion may not necessarily result in a stack overflow depending on the language, optimizations performed by the compiler and the exact structure of a code. In this case, the behavior of unreachable code (the return statement) is undefined, so the compiler can eliminate it and use a
tail call optimization that might result in no stack usage. Other optimizations could include translating the recursion into iteration, which, given the structure of the example function, would result in the program running forever, while probably not overflowing its stack. == See also ==