Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Segmentation fault
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Causes == The conditions under which segmentation violations occur and how they manifest themselves are specific to hardware and the operating system: different hardware raises different faults for given conditions, and different operating systems convert these to different signals that are passed on to processes. The proximate cause is a memory access violation, while the underlying cause is generally a [[software bug]] of some sort. Determining the [[Root cause analysis|root cause]] β [[debugging]] the bug β can be simple in some cases, where the program will consistently cause a segmentation fault (e.g., dereferencing a [[null pointer]]), while in other cases the bug can be difficult to reproduce and depend on memory allocation on each run (e.g., dereferencing a [[dangling pointer]]). The following are some typical causes of a segmentation fault: * Attempting to access a nonexistent memory address (outside process's address space) * Attempting to access memory the program does not have rights to (such as kernel structures in process context) * Attempting to write read-only memory (such as code segment) These in turn are often caused by programming errors that result in invalid memory access: * Dereferencing a [[null pointer]], which usually points to an address that's not part of the process's address space * Dereferencing or assigning to an uninitialized pointer ([[wild pointer]], which points to a random memory address) * Dereferencing or assigning to a freed pointer ([[dangling pointer]], which points to memory that has been freed/deallocated/deleted) * A [[buffer overflow]] * A [[stack overflow]] * Attempting to execute a program that does not compile correctly. (Some compilers{{Which?|date=December 2021}} will output an [[executable file]] despite the presence of compile-time errors.) In C code, segmentation faults most often occur because of errors in pointer use, particularly in [[C dynamic memory allocation]]. Dereferencing a null pointer, which results in [[undefined behavior]], will usually cause a segmentation fault. This is because a null pointer cannot be a valid memory address. On the other hand, wild pointers and dangling pointers point to memory that may or may not exist, and may or may not be readable or writable, and thus can result in transient bugs. For example: <syntaxhighlight lang=c> char *p1 = NULL; // Null pointer char *p2; // Wild pointer: not initialized at all. char *p3 = malloc(10 * sizeof(char)); // Initialized pointer to allocated memory // (assuming malloc did not fail) free(p3); // p3 is now a dangling pointer, as memory has been freed </syntaxhighlight> Dereferencing any of these variables could cause a segmentation fault: dereferencing the null pointer generally will cause a segfault, while reading from the wild pointer may instead result in random data but no segfault, and reading from the dangling pointer may result in valid data for a while, and then random data as it is overwritten.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)