linux中的段错误

1. 数组越界

C/C++中数组越界不会引起段错误,而且编译会通过

这样有可能会产生以下问题

  • 会覆盖掉已经使用的其他内存
  • 会访问有访问保护的内存
2. 段错误

Linux下会产生如下信息提示

在这里插入图片描述

段错误的定义

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.

段错误产生的原因

  • 访问了不存在的内存地址

    ​ 比如空指针

    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
    	int *ptr = NULL;
    	*ptr = 0;
    }
    
  • 访问系统保护的内存地址

    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
        int *ptr = (int *)0;
        *ptr = 100;
    }
    
  • 修改只读的内存地址的内容

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void main()
    {
        char *ptr = "test";
        ptr[0] = 'T';
        strcpy(ptr, "TEST");
    }
    
  • ​ 栈溢出

    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
    	main();
    }
    
3. 段错误信息获取

3.1 dmesg命令

​ dmesg可以在应用程序crash掉时,显示内核中保存的相关信息。如下所示,通过dmesg命令可以查看发生段错误的程序名称、引起段错误发生的内存地址、指令指针地址、堆栈指针地址、错误代码、错误原因等。

3.2 nm命令

​ 使用nm命令列出二进制文件中的符号表,包括符号地址、符号类型、符号名等,这样可以帮助定位在哪里发生了段错误。

3.3 ldd命令

​ 使用ldd命令查看二进制程序的共享链接库依赖,包括库的名称、起始地址,这样可以确定段错误到底是发生在了自己的程序中还是依赖的共享库中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值