没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Linux环境下段错误的产生原因及调试方法小结环境下段错误的产生原因及调试方法小结
借此机会系统学习了一下,这里对Linux环境下的段错误做个小结,方便以后同类问题的排查与解决
最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且项目工程庞大复杂,出现了不少问
题,其中遇到最多、花费时间最长的问题就是著名的“段错误”(Segmentation Fault)。借此机会系统学习了一下,这里对
Linux环境下的段错误做个小结,方便以后同类问题的排查与解决。
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>
#include<string.h>
void main()
{
char *ptr = "test";
strcpy(ptr, "TEST");
}
2.4 栈溢出
复制代码 代码如下:
#include<stdio.h>
#include<stdlib.h>
weixin_38723373
- 粉丝: 7
- 资源: 915
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页