喝黑咖啡有什么好处| 1号来月经什么时候是排卵期| 龟头炎是什么| 舌苔厚白是什么原因| 尿中有泡沫是什么原因| 手术后吃什么鱼伤口愈合快| 痔疮是什么样子| 一个三点水一个及念什么| 绿色裙子搭配什么颜色上衣| 调理月经吃什么药最好| 一点是什么时辰| 腿长身子短有什么说法| 一个叉念什么| 嗜酸性粒细胞是什么| 高脂血症是什么意思| 粘纤是什么材料| 什么品种的狗最粘人| 萨瓦迪卡什么意思| 更年期出汗吃什么药好| 韵五行属什么| 梦见生孩子是什么意思| 韧带拉伤用什么药好| touch是什么牌子| 前列腺增生是什么原因引起的| 嘴角周围长痘痘是什么原因| 前列腺增大钙化是什么意思| 法警是什么编制| 黄豆芽炒什么好吃| 哺乳期发烧吃什么药不影响哺乳| 血便是什么原因引起的| 2024年属龙的是什么命| 谷胱甘肽是什么| 反应蛋白测定是查什么的| 蓝莓树长什么样| 焯水是什么意思| 球蛋白偏低是什么原因| 老夫聊发少年狂什么意思| 什么是植物神经紊乱| 女性性冷淡是什么原因| 做梦翻车了什么预兆| 当兵有什么好处| 什么水果对肠胃好| 眉毛长长是什么原因| 紫菜是什么颜色| 什么是碱性食物有哪些| 奢靡是什么意思| da医学上是什么意思| 94年属于什么生肖| 一家之主是什么意思| 团粉是什么| 夏至是什么时候| 服软是什么意思| 蛇盘疮吃什么药好得快| 经期便秘是什么原因| rbc是什么意思医学| 什么叫阴虱| 丁香花长什么样| 高血压吃什么食物好| 脑委缩吃什么药能空制| 6月30日是什么日子| 春天有什么特点| 指导员是什么级别| 蛾子吃什么| 什么的旅行| a型血为什么叫贵族血| 康桑密达是什么意思| meq是什么单位| 初遇是什么意思| pigeon是什么意思| 卦不走空是什么意思| 东华帝君是什么神仙| 散光有什么症状| 澳门什么时候回归的| xpe是什么材料| 肚子突然疼是什么原因| 拉伤筋用什么药好| 月经量少吃什么药| 冥冥之中是什么意思| 动态密码是什么意思| 吃什么对牙齿有好处| 那是什么呢| 土中金是什么数字| cartoon什么意思| 六点半是什么时辰| 阿昔洛韦片治什么病| 胃结石有什么症状表现| 最近天气为什么这么热| 胆结石是什么原因造成的| 用什么洗脸可以祛斑| 月经期间喝酒会有什么影响| 前列腺增大有什么危害| 间接胆红素高说明什么| 土霉素主要是治疗什么病| 结婚10年是什么婚| 吃鱼有什么好处| 怀孕第一个月有什么症状| 天蝎和什么星座最配| 焦亚硫酸钠是什么| 7号来的月经什么时候是排卵期| 王母娘娘属什么生肖| 梦到男朋友出轨了预示什么意思| 极有家是什么意思| 什么是医美| 妈妈最大的愿望是什么| 文采是什么意思| 吃什么白细胞升的最快| 糖耐筛查主要检查什么| 坐月子可以喝什么饮料| 方解石玉是什么玉| 便秘和腹泻交替出现是什么意思| 张艺兴为什么不退出exo| 尿胆原norm是什么意思| 身强力壮是什么生肖| 总打喷嚏是什么原因| 土界读什么| 心脏房颤是什么意思| 燕子吃什么食物| bally属于什么档次| 拉肚子喝什么粥| 剃光头有什么好处| 爱恨就在一瞬间是什么歌| 鸿雁是什么意思| 刘禅属什么生肖| 垣什么意思| 仙代表什么生肖| 掉马是什么意思| 家是什么生肖| 木棉花什么时候开花| 空调什么度数最省电| 晚上9点是什么时辰| 吸烟有害健康为什么国家还生产烟| 看诊是什么意思| 什么药和酒一起吃必死| 结肠炎是什么原因引起的| 记仇的人是什么性格| 糖耐什么时候检查| 枝柯是什么意思| 肾痛是什么原因| 先天性巨结肠有什么症状| 乙肝疫苗什么时候打| 手腕扭伤挂什么科| 中秋吃什么| pab是什么意思| 孺子可教什么意思| 铠是什么意思| 舅舅是什么关系| 荔枝什么品种好吃| 叶凡为什么找石昊求救| 放风筝是什么季节| 年轻人长老年斑是什么原因| 宫寒是什么原因引起的如何调理| 刘邦属什么生肖| 做颈动脉彩超挂什么科| 强的松又叫什么| 狐臭用什么药| 贯众是什么植物| 雷震子是什么神| 明天什么考试| 胆水是什么| 杀手锏是什么意思| hippo什么意思| 12岁属什么| 血糖高吃什么好能降糖| 拉尼娜现象是什么| lirs 是什么意思| 癫疯病发作前兆有什么症状| 长瘊子是什么原因| 清心寡欲是什么意思| 丙型肝炎吃什么药最好| 火烧是什么食物| 争辩的近义词是什么| 格列本脲和格列美脲有什么区别| 手抖是什么病的症状| 苍耳是什么| 喝黄瓜汁有什么好处| 久卧伤什么| shit什么意思| 地主是什么意思| 又字五行属什么| 招财进宝是什么生肖| 右侧附件区囊性回声是什么意思| 高血压要吃什么| 居住证是什么意思| 莲雾什么味道| 出去旅游需要带什么| 婆娑是什么意思| 耳石症是什么| 汤姆福特属于什么档次| 什么是机械手表| swissmade是什么意思| 怀孕初期吃什么好| 为什么会得卵巢肿瘤| 梦见大老鼠是什么意思| 孕期吃什么好| 妈祖是什么| 甲醇对人体有什么伤害| 后背酸疼是什么原因| 手臂长痘痘是什么原因| 鞘膜积液挂什么科| 什么的朝霞| fsh是什么意思| 什么蛋白质含量高| 吃什么养胃又补胃| 梦见怀孕流产是什么意思| 走读生是什么意思| 人中龙凤下一句是什么| 胃胀反酸吃什么药效果好| 情感障碍是什么| 什么叫引流| 慕字五行属什么| 追随是什么意思| ad医学上是什么意思| 什么东西有助于睡眠| 53年属什么| 1960年属鼠的是什么命| 土耳其是什么人种| 鞠婧祎什么星座| 斯里兰卡用什么货币| tsh代表什么| 发际线长痘痘是什么原因| 大便硬是什么原因| 89年属蛇是什么命| 什么是党的性质和宗旨的体现| 退而求其次是什么意思| 什么是肽| 万箭穿心代表什么生肖| 生理期什么意思| 红房子是什么| 旺字五行属什么| 四季常青财运旺是什么生肖| 拔罐后发痒是什么原因| 百鸟归巢什么意思| 甘油三酯高吃什么食物降得快| 三生石是什么意思| 总蛋白低是什么意思| 容易水肿是什么原因| 双侧筛窦粘膜增厚是什么意思| 兔死狗烹什么意思| 精神病人最怕什么刺激| 肺结节不能吃什么食物| 在什么的前面用英语怎么说| 一什么门牙| 棕榈油是什么油| 滇红茶属于什么茶| 孕妇适合喝什么汤| 梦到羊是什么意思| 草代表什么生肖| 一什么| sla是什么| 嘴里起泡是什么原因| 锁骨是什么骨| 为什么得甲亢| 日光性皮炎用什么药膏最有效| 孕激素六项检查什么时候做| 1月18是什么星座| 为什么会胃酸反流| 羲什么意思| 用什么可以美白| 经期喝咖啡有什么影响| 幽门螺杆菌感染有什么症状和表现| 健康证要带什么| 为什么歌曲| 老抽和生抽有什么区别| 胆固醇偏高有什么危害| 百度Jump to content

望子成龙是什么意思

From Wikipedia, the free encyclopedia
百度 将综合网友推荐(自荐)、编辑提名出30个博客候选,由所有强国社区用户投票选举出2013年最具影响力的十大博客。

In computing, a segmentation fault (often shortened to segfault) or access violation is a failure condition raised by hardware with memory protection, notifying an operating system (OS) that the software has attempted to access a restricted area of memory (a memory access violation). On standard x86 computers, this is a form of general protection fault. The operating system kernel will, in response, usually perform some corrective action, generally passing the fault on to the offending process by sending the process a signal. Processes can in some cases install a custom signal handler, allowing them to recover on their own,[1] but otherwise the OS default signal handler is used, generally causing abnormal termination of the process (a program crash), and sometimes a core dump.

Segmentation faults are a common class of error in programs written in languages like C that provide low-level memory access and few to no safety checks. They arise primarily due to errors in use of pointers for virtual memory addressing, particularly illegal access. Another type of memory access error is a bus error, which also has various causes, but is today much rarer; these occur primarily due to incorrect physical memory addressing, or due to misaligned memory access – these are memory references that the hardware cannot address, rather than references that a process is not allowed to address.

Many programming languages have mechanisms designed to avoid segmentation faults and improve memory safety. For example, Rust employs an ownership-based[2] model to ensure memory safety.[3] Other languages, such as Lisp and Java, employ garbage collection,[4] which avoids certain classes of memory errors that could lead to segmentation faults.[5]

Overview

[edit]
Example of human generated signal
Segmentation fault affecting Krita in KDE desktop environment
A null pointer dereference on Windows 8

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

The term "segmentation" has various uses in computing; in the context of "segmentation fault", it refers to the address space of a program.[6] With memory protection, only the program's own address space is readable, and of this, only the stack and the read/write portion of the data segment of a program are writable, while read-only data allocated in the const segment and the code segment are not writable. Thus attempting to read outside of the program's address space, or writing to a read-only segment of the address space, results in a segmentation fault, hence the name.

On systems using hardware memory segmentation to provide virtual memory, a segmentation fault occurs when the hardware detects an attempt to refer to a non-existent segment, or to refer to a location outside the bounds of a segment, or to refer to a location in a fashion not allowed by the permissions granted for that segment. On systems using only paging, an invalid page fault generally leads to a segmentation fault, and segmentation faults and page faults are both faults raised by the virtual memory management system. Segmentation faults can also occur independently of page faults: illegal access to a valid page is a segmentation fault, but not an invalid page fault, and segmentation faults can occur in the middle of a page (hence no page fault), for example in a buffer overflow that stays within a page but illegally overwrites memory.

At the hardware level, the fault is initially raised by the memory management unit (MMU) on illegal access (if the referenced memory exists), as part of its memory protection feature, or an invalid page fault (if the referenced memory does not exist). If the problem is not an invalid logical address but instead an invalid physical address, a bus error is raised instead, though these are not always distinguished.

At the operating system level, this fault is caught and a signal is passed on to the offending process, activating the process's handler for that signal. Different operating systems have different signal names to indicate that a segmentation fault has occurred. On Unix-like operating systems, a signal called SIGSEGV (abbreviated from segmentation violation) is sent to the offending process. On Microsoft Windows, the offending process receives a STATUS_ACCESS_VIOLATION exception.

Causes

[edit]

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 causedebugging 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?] 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:

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

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.

Handling

[edit]

The default action for a segmentation fault or bus error is abnormal termination of the process that triggered it. A core file may be generated to aid debugging, and other platform-dependent actions may also be performed. For example, Linux systems using the grsecurity patch may log SIGSEGV signals in order to monitor for possible intrusion attempts using buffer overflows.

On some systems, like Linux and Windows, it is possible for the program itself to handle a segmentation fault.[7] Depending on the architecture and operating system, the running program can not only handle the event but may extract some information about its state like getting a stack trace, processor register values, the line of the source code when it was triggered, memory address that was invalidly accessed[8] and whether the action was a read or a write.[9]

Although a segmentation fault generally means that the program has a bug that needs fixing, it is also possible to intentionally cause such failure for the purposes of testing, debugging and also to emulate platforms where direct access to memory is needed. On the latter case, the system must be able to allow the program to run even after the fault occurs. In this case, when the system allows, it is possible to handle the event and increment the processor program counter to "jump" over the failing instruction to continue the execution.[10]

Examples

[edit]
Segmentation fault on an EMV keypad

Writing to read-only memory

[edit]

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 "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 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:

$ 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,[11][12][13] 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

[edit]

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

[edit]

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

[edit]

Another example is recursion without a base case:

int main(void)
{
    return main();
}

which causes the stack to overflow which results in a segmentation fault.[14] 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

[edit]

References

[edit]
  1. ^ Expert C programming: deep C secrets By Peter Van der Linden, page 188
  2. ^ "The Rust Programming Language - Ownership".
  3. ^ "Fearless Concurrency with Rust - The Rust Programming Language Blog".
  4. ^ McCarthy, John (April 1960). "Recursive functions of symbolic expressions and their computation by machine, Part I". Communications of the ACM. 4 (3): 184–195. doi:10.1145/367177.367199. S2CID 1489409. Retrieved 2025-08-05.
  5. ^ Dhurjati, Dinakar; Kowshik, Sumant; Adve, Vikram; Lattner, Chris (1 January 2003). "Memory safety without runtime checks or garbage collection" (PDF). Proceedings of the 2003 ACM SIGPLAN conference on Language, compiler, and tool for embedded systems. Vol. 38. ACM. pp. 69–80. doi:10.1145/780732.780743. ISBN 1581136471. S2CID 1459540. Retrieved 2025-08-05.
  6. ^ "Debugging Segmentation Faults and Pointer Problems - Cprogramming.com". www.cprogramming.com. Retrieved 2025-08-05.
  7. ^ "Cleanly recovering from Segfaults under Windows and Linux (32-bit, x86)". Retrieved 2025-08-05.
  8. ^ "Implementation of the SIGSEGV/SIGABRT handler which prints the debug stack trace". GitHub. Retrieved 2025-08-05.
  9. ^ "How to identify read or write operations of page fault when using sigaction handler on SIGSEGV?(LINUX)". Retrieved 2025-08-05.
  10. ^ "LINUX – WRITING FAULT HANDLERS". 12 November 2017. Retrieved 2025-08-05.
  11. ^ "6.1.4 String literals". ISO/IEC 9899:1990 - Programming languages -- C.
  12. ^ "6.4.5 String literals". ISO/IEC 9899:1999 - Programming languages -- C.
  13. ^ "6.4.5 String literals". ISO/IEC 9899:2011 - Programming languages -- C.
  14. ^ "What is the difference between a segmentation fault and a stack overflow?". Stack Overflow. Retrieved 2025-08-05.
[edit]
年少轻狂下一句是什么 吃什么对肝好怎么养肝 肝脏不好吃什么食物才能养肝护肝 维生素b不能和什么一起吃 是什么字
铁剂不能和什么一起吃 检查视力挂什么科 宫崎骏是什么意思 人为什么会打嗝 梅毒挂什么科
咖啡有什么作用和功效 免疫五项能查出什么病 发烧呕吐吃什么药 肾阴虚吃什么食物补 呕气是什么意思
偶尔什么意思 hx是什么 55年出生属什么 机能鞋是什么意思 黄芪长什么样子
十月初八是什么星座hcv9jop6ns0r.cn 家里为什么有蚂蚁hcv9jop1ns9r.cn 草字头加果念什么hcv9jop5ns9r.cn 怀孕有什么特征和反应weuuu.com 反刍什么意思hcv9jop5ns8r.cn
小孩子肚子疼吃什么药hcv8jop2ns0r.cn scj是什么意思hcv9jop5ns3r.cn 什么是功德huizhijixie.com 脚气缺什么维生素hcv7jop5ns4r.cn 地藏王菩萨是管什么的cl108k.com
桥字五行属什么hcv9jop3ns7r.cn 鳞状上皮乳头状瘤是什么hcv9jop5ns7r.cn 小甲鱼吃什么hcv8jop9ns4r.cn 醒酒汤是什么hcv8jop8ns7r.cn 小腿疼痛为什么hcv8jop5ns4r.cn
什么是遴选96micro.com 足齐念什么1949doufunao.com 雨后的彩虹像什么hcv8jop1ns6r.cn 产后抑郁一般发生在产后什么时间hcv9jop7ns2r.cn 宫腔积液是什么hcv7jop6ns4r.cn
百度