在我们的linux嵌入式应用程序中,会有各种各样的死机问题(我这里提到的是用户态),出现死机的原因也是很多,其中中间一种最常见的一种就是段错误啦。
1. 段错误是什么
段错误是指访问的内存超出了系统给这个程序所设定的内存空间,例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。下面是标准的解释,大家可以看下
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.
下面我列出几种最为常见的几种出现段错误的情况
1,访问了不该访问的地址,比如野指针,对空指针进行操作
2,内存越界,这里面包括data数据段的越界,heap区的越界,还有bss段的越界。