bus error与segment error

本文深入解析C程序中常见的段错误和总线错误。段错误通常源于对NULL指针的解引用或访问非法内存地址;总线错误则由对未对齐地址的不当访问导致。文章提供了理解这些错误背后的原理及解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在c程序中,经常会遇到段错误(segment error)和总线错误(bus error),这两种问题出现的原因可能如下

  • 段错误:
    1. 对一个NULL指针解引用。
    2. 访问程序进程以外的内存空间。

  实际上,第一个原因可以规约到第二个原因,在一个c程序的虚拟内存空间中,从低地址到高地址一次是代码区,堆区(向上增长),栈区(向下增长),最上是常量区,其中NULL指针的位置正好是虚拟内存中地址为0的位置,而这个位置是不属于以上4个区域的,同理一些比较低的地址也不属于这四个区,所以造成段错误的原因是访问了程序虚拟内存空间4个区以外的地址,在平时的开发中,最大的可能还是对NULL进行了解引用。

  • 总线错误
    1. 对一个错误的起始地址进行解引用

  由于进程的虚拟内存空间实际上是对物理地址的一个映射,操作系统和编译器会用内存对齐来做优化,通常就是4字节对齐,所以int,float这种类型的起始地址都是4的倍数,而short的起始地址是2的倍数,double的起始地址是8的倍数,假如此时对一个不是4倍数的地址a进行解引用 (int *) a,就可能会出现总线错误,这个出现的情况还要具体看是哪一种操作系统。总线错误一般不会出现,出现的情况多半是使用了指针的强制转换。

  有了以上分析就能在遇到这些问题时有处理的思路了。




来自:http://www.cnblogs.com/xpray/p/3491186.html

2025-07-19 19:43:21.197885 cooling_app ERROR: cooling_app.c(815): cooling_os_boot_policyclass_process: set fan speed: os_start_times[122] level[60] 2025-07-19 19:43:26.800816 card_manage ERROR: pcie_card_parse_bdf_thread.c(156): pcie_bdf_parse: OCPCardBDF slot1, cpuid:0x0, segment:0x0 bus:0x16 dev:0x0 func:0x0 2025-07-19 19:43:26.801244 card_manage ERROR: pcie_card_parse_bdf_thread.c(156): pcie_bdf_parse: OCPCardBDF slot2, cpuid:0x1, segment:0x0 bus:0xb8 dev:0x0 func:0x0 2025-07-19 19:43:26.818438 card_manage ERROR: pcie_card_parse_bdf_thread.c(156): pcie_bdf_parse: PcieCardBDF slot1, cpuid:0x0, segment:0x0 bus:0x38 dev:0x0 func:0x0 2025-07-19 19:43:26.819543 card_manage ERROR: pcie_card_parse_bdf_thread.c(156): pcie_bdf_parse: PcieCardBDF slot2, cpuid:0x0, segment:0x0 bus:0x27 dev:0x0 func:0x0 2025-07-19 19:43:28.905957 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 20 failed 2025-07-19 19:43:29.007203 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 21 failed 2025-07-19 19:43:29.108530 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 24 failed 2025-07-19 19:43:29.209835 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 25 failed 2025-07-19 19:43:29.311451 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 26 failed 2025-07-19 19:43:29.412780 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 27 failed 2025-07-19 19:43:29.514013 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 30 failed 2025-07-19 19:43:29.615247 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 33 failed 2025-07-19 19:43:29.716558 diagnose ERROR: fdm_io_addr_update.c(673): find group_id 1 com_type 39, slot 37 failed 2025-07-19 19:43:29.862415 card_manage ERROR: pcie_card_parse_bdf_thread.c(526): pcie_card_xml_load: type1 index0, slot1, 0x15b31017, 0x15b31017, 0x1f242006, 0x1f242006 2025-07-19 19:43:29.862888 card_manage ERROR: pcie_card_parse_bdf_thread.c(526): pcie_card_xml_load: type1 index1, slot2, 0x15b31017, 0x15b31017, 0x1f242006, 0x1f242006 2025-07-19 19:43:33.040132 card_manage ERROR: pcie_card_update_info.c(1647): pcie_card_get_class_info:get class info :pcie_index=0 class=0x02 subClass=0x00 2025-07-19 19:43:33.060444 card_manage ERROR: pcie_card_update_info.c(1647): pcie_card_get_class_info:get class info :pcie_index=1 class=0x02 subClass=0x00 2025-07-19 19:43:33.075259 CpuMem : ERROR: cpu_monitor.c(699): cpu_pci_configuration_read failed! (repeated 9 times)
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值