c\c++ BUG Segmentation fault 段错误

起初是我写朋友课设遇到的问题,跑程序起来发现无法读取链表信息到文件里,换了一个ide后发现是fscanf报错Segmentation fault 查询资料发现是段错误

那么什么是段错误呢

下面是来自Answers.com的定义:

A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. In short, 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 (e.g., attempts to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as Address or Bus errors.

Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.

On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.

翻译后是:

 

所谓的段错误 就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存器,其中的32位是保存由它指向的gdt表, 后13位保存相应于gdt的下标,最后3位包括了程序是否在内存中以及程序的在cpu中的运行级别,指向的gdt是由以64位为一个单位的表,在这张表中 就保存着程序运行的代码段以及数据段的起始地址以及与此相应的段限和页面交换还有程序运行级别还有内存粒度等等的信息。一旦一个程序发生了越界访 问,cpu就会产生相应的异常保护,于是segmentation fault就出现了

 

可以分析出,段错误应该就是访问了不可访问的内存,这个内存区要么是不存在的,要么是受到系统保护的。

我也去阅读了其他博客

中国linux论坛有一篇精华帖子《Segment fault 之永远的痛》:http://www.linuxforum.net/forum/gshowflat.php?Cat=&Board=program&Number=193239&page=2&view=collapsed&sb=5&o=all&fpart=1&vc=1

现在的我看技术文还是有点吃力。但是初步了解了段错误的原因,也解决了问题

下面是问题图片

c\c++ BUG Segmentation fault 段错误

c\c++ BUG Segmentation fault 段错误

和解决后图片

c\c++ BUG Segmentation fault 段错误

最后贴上寻找问题开的网页

c\c++ BUG Segmentation fault 段错误

亏贼

 

原文链接: https://www.cnblogs.com/jszs0013/p/17100106.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c\c++ BUG Segmentation fault 段错误

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/316502

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午2:16
下一篇 2023年2月16日 下午2:17

相关推荐