分享

Bus Error and segment fault

 todaytomo 2007-07-08

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  ++;
                }
     }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多