Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Segmentation fault
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Segmentation fault
(section)
Add topic