Linux C程序:Segmentation fault

段错误

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

英文简介

   A segmentation fault (often shortened to segfault) is a particular error condi
  tion that can occur during the operation of computer software. In short, a seg
  mentation fault occurs when a program attempts to access a memory location tha
  t it is not allowed to access, or attempts to access a memory location in a wa
  y 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 operat
  ing system. It has been superseded by paging for most purposes, but much of th
  e terminology of segmentation is still used, "segmentation fault" being an exa
  mple. Some operating systems still have segmentation at some logical level alt
  hough paging is used as the main memory management policy.
  On Unix-like operating systems, a process that accesses invalid memory receive
  s the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid me
  mory receives the STATUS_ACCESS_VIOLATION exception.
  另外,这里有个基本上对照的中文解释,来自库扩展阅读1;
   Quote:
  一旦一个程序发生了 越界访问, cpu就会产生相应的保护,于是segmentation fault就出现
  了通过上面的解释,段 错误应该就是访问了不可访问的内存,这个 内存区要么是不存在的,
  要么是受到系统保护的,还有可能是缺少文件或者文件损坏。寻找程序安装维修按列,

段错误的常见形式

  在编程中以下几类做法容易导致段错误,基本是是错误地使用 指针引起的
  1)访问系统数据区,尤其是往 系统保护的 内存地址数据最常见就是给一个指针以0地址
  2)内存越界(数组越界,变量类型不一致等) 访问到不属于你的内存区域解决方法我们在用C/ C++语言写程序的时侯, 内存管理的绝大部分工作都是需要我们来做的。实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难 免会在此处犯些小错误,而通常这些错误又是那么的浅显而易于消除。但是手工“除虫”(debug),往往是效率低下且让人厌烦的,本文将就"段错误"这个 内存访问越界的错误谈谈如何快速定位这些"段错误"的语句。
  下面将就以下的一个存在段错误的程序介绍几种调试方法:
  
1 dummy_function (void)
2 {
3 unsigned char *ptr = 0x00;
4 *ptr = 0x00;
5 }
6
7 int main (void)
8 {
9 dummy_function ();
10
11 return 0;
12 }
  作为一个熟练的C/C++程序员,以上代码的bug应该是很清楚的,因为它尝试操作地址为0的内存区域,而这个内存区域通常是不可访问的禁区,当然就会出错了。我们尝试编译运行它:
  
xiaosuo@gentux test $ ./a.out
段错误
  果然不出所料,它出错并退出了。
  1.利用gdb逐步查找段错误:
  这种方法也是被大众所熟知并广泛采用的方法,首先我们需要一个带有调试信息的可执行程序,所以我们加上“-g -rdynamic"的参数进行编译,然后用gdb调试运行这个新编译的程序,具体步骤如下:
  
xiaosuo@gentux test $ gcc -g -rdynamic d.c
xiaosuo@gentux test $ gdb ./a.out
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread
(gdb) r
Starting program: /home/xiaosuo/test/a.out
Program received signal SIGSEGV, Segmentation fault.
0x08048524 in dummy_function () at d.c:4
4 *ptr = 0x00;
(gdb)
  哦?!好像不用一步步调试我们就找到了出错位置d.c文件的第4行,其实就是如此的简单。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

長安只在旧夢中

知识的大门打开后,才有真正机会

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值