Bus error (Fieldbus errors)

Source: Internet
Author: User

Transfer from http://blog.csdn.net/todd911/article/details/8813321

Bus errors (core dumped) are mentioned in "C Expert programming".

Bus errors are almost always caused by misaligned reads or writes.

It is called a bus error because the blocked component is address bus when there is an unaligned memory access request. Alignment means that a data item can only be stored in a memory location where the address is an integer multiple of the size of the data item.

Modern computer architectures, especially RISC architectures, require word alignment because the extra logic associated with arbitrary alignment makes the memory system larger and slower.

By forcing each memory access to a single cache line or a separate page, you can greatly simplify (and accelerate) hardware such as the cache controller and memory management unit.

The size of the page and cache is carefully designed so that an atomic data item does not cross the boundary of a page or cache block as long as the alignment rules are adhered to.

Examples of bus errors are also given in the book:

[CPP]View Plaincopy
    1. Union
    2. {
    3. Char a[10];
    4. int i;
    5. }u;
    6. int *p = (int*) & (U.a[1]);
    7. *p = 17; An unaligned address in the/*p will cause a bus error because the union of the array and int ensures that a is aligned by 4 bytes of int, so "a+1" must not be aligned with int. */  

However, this error does not occur in the actual operation, my environment is CentOS release 6.2,2.6.32-279.14.1.EL6.I686,GCC 4.4.6

Later on the online reference to a sample program, the program is modified as follows:

[CPP]View Plaincopy
  1. #include <stdlib.h>
  2. int main (int argc, char **argv) {
  3. #if defined (__gnuc__)
  4. # if defined (__i386__)
  5. / * Enable Alignment Checking on x86 * /
  6. __asm__ ("Pushf\norl $0x40000, (%ESP) \npopf");
  7. # elif defined (__x86_64__)
  8. / * Enable Alignment Checking on x86_64 * /
  9. __asm__ ("Pushf\norl $0x40000, (%RSP) \npopf");
  10. # endif
  11. #endif
  12. union{
  13. Char a[10];
  14. int i;
  15. }u;
  16. int *p = (int*) & (U.a[1]);
  17. *p = 17;
  18. }

The results of the operation are as follows:
Bus error (Core dumped)

The reasons are:

The x86 architecture aligns the addresses, accesses two times, and then puts the first tail and the second head together.

If it were not for x86, the machine under that architecture would not automatically do the work, and it would produce a core.

If the alignment check function is turned on in the code, the bus error will be displayed after running

Bus error (Fieldbus errors)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.