Linux下gcc编译程序出现“段错误”

这两天在Github上找到了一个开源的project可以来拿为我的相关研究工作所使用,不过需要进行二次开发,于是狠下心来啃这个project的代码。

等到终于啃完代码了,了解了程序的意图,于是想着就先让其跑起来试试,可是一运行就各种错误,不过大部分的错误在调试过程中慢慢就被消灭了,

只剩下一个叫做Fragementation Fault的问题,完全没有头绪啊,提示信息也不够,另外本人对汇编不熟,已经多年不碰它了,实在无法靠一己之力找出问题,

于是便在万能的百度上寻找答案,翻阅了众多博客之后发现一篇写fragmentation fault挺不错的文章,于是引用过来,以备以后不时之需。

 

感谢原作者的分享,以下的内容转载于:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html

1. 段错误是什么

一句话来说,段错误是指访问的内存超出了系统给这个程序所设定的内存空间,例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。这里贴一个对于“段错误”的准确定义(参考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.
复制代码

2. 段错误产生的原因

2.1 访问不存在的内存地址

复制代码
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr = NULL;
*ptr = 0;
}
复制代码

2.2 访问系统保护的内存地址

复制代码
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr = (int *)0;
*ptr = 100;
}
复制代码

2.3 访问只读的内存地址

复制代码
#include<stdio.h>
#include<stdlib.h>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值