打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Bus Error and segment fault

As for your question, the C standard does not define either "bus
error" or "segmentation fault", these are things that happen with your
particular compiler on your particular operating system.

In general, they are both the result of something wrong in your
program, an error that produces undefined behavior. When your program
generates undefined behavior C no longer specifies what might happen,
and things like "bus error" and "segmentation faults" are the results
of certain types of undefined behavior on your particular system.

To find out for sure you need to ask in a group that supports your
particular compiler/OS combination.

For several common such combinations, a segmentation fault results
when your program tries to access memory that does not belong to it,
for example with an uninitialized or null pointer, or writes past the
end of allocated memory. Bus faults generally result when you play
games with pointer conversions and access a variable using a pointer
with incorrect alignment.

Segmentation fault
Bus error
These runtime messages indicate a memory access error. Common causes include:
  • dereferencing a null pointer or uninitialized pointer
  • out-of-bounds array access
  • incorrect use of malloc, free and related functions
  • use of scanf with invalid arguments
There is a subtle difference between segmentation faults and bus errors. A segmentation fault occurs when a process tries to access memory protected by the operating system. A bus error occurs when valid memory is accessed in an incorrect way (for example, trying to read an unaligned value on architectures where values must be aligned with 4-byte offsets).
in my practice, when


        common_record_t_new data_record_a ;
    *** common_record_t_new *data_record = &data_record_a;

        data_record->prot = 17;
        data_record->dir = 1;

        length = getpagesize();
        length = length << 14;
        printf ("the length is %d\n", length);

        fd = open("foo", O_RDWR|O_CREAT|O_TRUNC, 00777);
        if (-1 == fd){
                printf("open file foo failed!\n");
                exit(0) ;
        }
        /*lseek(fd, length, SEEK_SET);
        write(fd, "", 1);*/

        //mstart = mmap((caddr_t)0, length, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
        out = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if  no ***, then segment fault because of no iniatializtion for pointer data_record;
    if no lseek and write , then the file "foo" is empty, so can‘t mmap it to memory. and then bus error.
    when use read/write system call, we need a buffer to buffer the data, so
                    read (fd, buff, length);
    and previousely, we need declare a buffer like
                   char * buff;
     but if we just declare like above, and then "read", we will get the segment fault!
     why?
    just as above ***, we declare a buffer (or a pointer), but will definate it, so we need allocate a space for it, like
                  buff = (char *) malloc(length);

   in other example nfdump-new.c, we i try to visit the memory out of the range, then system push "segment fault!", just as:
  
    buff_size = COUNT * sizeof(common_record_t_new);
    common_record_new_t * nf_buff = malloc(buff_size);
    common_record_new_t * nf_record = nf_buff;

 
   while(!done)
   {
             read (nf_fd, nf_buff, buff_size);
                for (i = 0; i < COUNT; i++)
                {
                         print_record(nf_record);
                         nf_record  ++;
                }
     }
   so, the red code "nf_record ++" will exceed the range of available memory, then the system push the "segment fault".
  we need initialize the "nf_record" after "read":

while(!done)
   {
             read (nf_fd, nf_buff, buff_size);
             nf_record = nf_buff;
                for (i = 0; i < COUNT; i++)
                {
                         print_record(nf_record);
                         //nf_record  ++;
                }
     }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
"Working in the Protected Mode Environment"
Linux 文件映射mmap,Bus Error
segment fault、bus error错误测试
调试备忘录-J-Link RTT的使用(原理 + 教程 + 应用 + 代码)
mistake、error?、fault?和wrong?的区别
mmap bus error
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服