在Linux内存管理中,Linux结合了CPU架构采用了分段机制;
分段就是将内存分成大小不同的段空间,将进程之间和进程内部不同数据段之间隔离起来;
当程序内存的数据的访问超出了系统所给这个程序数据段的范围,系统就会给进程发送一个信号SIGSEGV,程序将终止退出。
所以,可以说段错误都是对内存操作错误引起的。
这里列举了常见的段错误:
1.访问不存在的内存、访问未知的受保护的内存;
2.访问未初始化的指针;
3.写常量区(只读区);
4.在栈中定义过大的数组,导致进程的栈空间不足;
5.数据类型格式转换错误;
6.4G的buffer会导致IO出现段错误
7.main函数递归调用导致栈溢出;
8.数组读写越界;(不一定会导致段错误,但一定会踩(覆盖)了其它数据)
9.使用局部变量返回值;(错误未知,有可能产生断错误)
10.使用已经释放的堆内存;(错误未知,有可能产生断错误)
11.当堆内存耗尽?
其它:
重复free,delete内存,程序将收到信号SIGABRT,然后退出。
<pre class="cpp" name="code">#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <signal.h>
//1.空指针解引用,访问不存在的内存地址
//访问一段未知、(非法)内存
void test_null_pointer()
{
char *p = NULL;
char c = *p;
int *p2 = (int*)0xffff;
*p2;
}
//传入不存在的内存给strlen
void test_strlen()
{
//int *p = NULL;
char *p;
cout<<strlen(p)<<endl; //Segmentation fault, 不能将NULL, /0, 0传入strlen
}
struct sInt
{
int a;
int *p;
};
//指针访问非法内存
void test_violation()
{
sInt si;
si.p = &si.a;
/*
si.p[0] = 0;
si.p[1] = 1;
si.p[2] = 2; //(覆盖了原指针的值)对指针重新赋值
si.p[3] = 3; //访问非法内存
*/
*(si.p) = 0;
*(si.p+1) = 1;
*(si.p+2) = 2;
*(si.p+3) = 3;
}
//2.访问未初始化指针
void test_unknown()
{
int *p;
*p = 100;
}
//3.写常量区
void test_const()
{
char * p = "hello, world";
p[0]= 'x';
}
//4.在栈中定义过大的数组,导致进程的栈空间不足,出现段错误
void test_stack()
{
float array[99][99][999] = {0};
}
//5.数据类型格式转换错误
void test_datatype_format()
{
//enum Week {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};
//enum Week day = TUESDAY;
int day = 1;
printf("monday: %s \n",day);
}
//6.4G的buffer导致io出现段错误
void test_malloc_4g()
{
char * buf;
buf = (char*)malloc(1<<31); //1<<31 4g
fgets(buf, 1024, stdin);
printf("%s \n", buf);
}
//7.数组越界
//数组越界不一定会导致段错误,但会踩(覆盖)了其它数据
void test_array_breakboundary()
{
char a[1];
strcpy(a,"hello,world !");
}
char * temp_pointer_return()
{
//char p[] = "xyz";
//return p;
char t = 'a';
return &t;
}
//8.使用局部变量返回值
void test_temp_pointer()
{
char *p = temp_pointer_return();
p[0] = 'a';
p[1] = 'a';
p[2] = 'a';
}
//9.使用已经释放的堆内存
void test_free_reuse()
{
char *p = (char*)malloc(sizeof(char));
free(p);
*p = 'a';
}
//10.栈溢出,递归调用main()
int main();
void test_main()
{
static int i = 0;
++i;
cout<<i<<endl;
main();
}
//堆内存耗尽
void test_memory_out()
{
for(int i=0; i<0xffffffff;i++)
{
char *p = (char*)malloc(1<<31);
cout<<i<<endl;
}
}
//Aborted
void test_delete()
{
char *p = new char;
delete p;
delete p;
}
//Aborted
void test_free()
{
char *p = (char*)malloc(sizeof(char));
free(p);
free(p);
}
void catchSegFault(int )
{
cout<<"Hello, Segmentation fault!"<<endl;
exit(0);
}
int main()
{
//signal(SIGSEGV, catchSegFault);
//test_null_pointer();
//test_strlen();
//test_violation();
//test_unknown();
//test_const();
//test_stack();
test_datatype_format();
//test_malloc_4g();
//test_array_breakboundary();
//test_temp_pointer();
//test_free_reuse();
//test_main();
//test_memory_out();
//test_delete();
//test_free();
return 1;
}