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!
=== 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 [[compiler]]s will not catch this at compile time, and instead compile this to executable code that will crash: <syntaxhighlight lang=c> int main(void) { char *s = "hello world"; *s = 'H'; } </syntaxhighlight> When the program containing this code is compiled, the string "hello world" 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 (programming)|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 ''H'' 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]]: <syntaxhighlight lang="console"> $ gcc segfault.c -g -o segfault $ ./segfault Segmentation fault </syntaxhighlight> [[Backtrace]] of the core file from [[GDB]]: <syntaxhighlight lang=c> Program received signal SIGSEGV, Segmentation fault. 0x1c0005c2 in main () at segfault.c:6 6 *s = 'H'; </syntaxhighlight> 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: <syntaxhighlight lang=c> char s[] = "hello world"; s[0] = 'H'; // equivalently, *s = 'H'; </syntaxhighlight> Even though string literals should not be modified (this has undefined behavior in the C standard), in C they are of <code>static char []</code> type,<ref>{{cite book|title=ISO/IEC 9899:1990 - Programming languages -- C|section=6.1.4 String literals}}</ref><ref>{{cite book|title=ISO/IEC 9899:1999 - Programming languages -- C|section=6.4.5 String literals}}</ref><ref>{{cite book|title=ISO/IEC 9899:2011 - Programming languages -- C|section=6.4.5 String literals|url=http://www.iso-9899.info/n1570.html#6.4.5p6}}</ref> so there is no implicit conversion in the original code (which points a <code>char *</code> at that array), while in C++ they are of <code>static const char []</code> type, and thus there is an implicit conversion, so compilers will generally catch this particular error.
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)