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!
=== 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.
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