bus error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    bus error

    Hi Everyone,

    I read an article which claims that an invalid address ( not aligned
    in power of two ) would cause a bus error exception, i tried the
    following program and it didn't give any error,

    int main()
    {
    int a = 10;
    char *p = &a;
    int *pp;
    printf("%p\n",( void*)&a);
    ++p;
    *p = 10;
    pp = p;
    printf("pp is %p and val %d\n",pp,*pp);
    }

    Am i missing something? How do i generate the bus error ?

    Thanks in advance ! ! !
  • Richard Tobin

    #2
    Re: bus error

    In article <7a5a24d4-86eb-4545-9c10-915d26deaecf@a2 3g2000hsc.googl egroups.com>,
    Rahul <sam_cit@yahoo. co.inwrote:
    I read an article which claims that an invalid address ( not aligned
    >in power of two ) would cause a bus error exception, i tried the
    >following program and it didn't give any error,
    You don't really mean "power of two". Processors that require
    alignment usually require alignment on a multiple of the object size.

    Some processors require alignment, some don't. Presumably you're
    using one that doesn't, probably something in the x86 family.

    Processors that require alignment do so for efficiency and simplicity
    reasons: an aligned object can be read or written in a single access,
    and can't cross page boundaries. From the viewpoint of most C
    programmers, it has the advantage that it often catches certain errors
    sooner than would happen otherwise. On the other hand, it has the
    disadvantage of wasting space between objects of different sizes, and
    making it hard to match data structures directly to external
    representations .

    -- Richard
    --
    :wq

    Comment

    • Ulrich Eckhardt

      #3
      Re: bus error

      Rahul wrote:
      I read an article which claims that an invalid address ( not aligned
      in power of two ) would cause a bus error exception
      That by itself is wrong. Rather, an unaligned access causes a bus error. An
      unaligned access is e.g. accessing a 2-byte value on an odd address, but
      not accessing a single byte on the same address. However, on the very
      popular x86 platform, this error doesn't even come through to the OS, let
      alone the program that caused it. Rather, the unaligned access is emulated
      by the CPU, and the only way to see it is to watch for loss of performance.
      I guess running on an x86 is the reason that you will never see this error.

      That said, this behaviour is specific to both the CPU and the OS (it could
      also emulate the access transparently in software!), none of which is
      actually mandated by the C language, so I would suggest further discussion
      in a different newsgroup.

      Uli

      Comment

      • David Thompson

        #4
        Re: bus error

        On 26 Mar 2008 15:38:11 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
        wrote:
        In article <7a5a24d4-86eb-4545-9c10-915d26deaecf@a2 3g2000hsc.googl egroups.com>,
        Rahul <sam_cit@yahoo. co.inwrote:
        >
        I read an article which claims that an invalid address ( not aligned
        in power of two ) would cause a bus error exception, i tried the
        following program and it didn't give any error,
        >
        You don't really mean "power of two". Processors that require
        alignment usually require alignment on a multiple of the object size.
        >
        Technically, but the sizes of primitive types almost always are powers
        of two: 2, 4, 8, or 16 bytes. (On byte machines, which nowadays are
        all 8 bits.) This is the sort of alignment that actually allows
        hardware advantages; multiples of 5, while formally similar, would in
        practice be worthless and thus not used.
        Some processors require alignment, some don't. Presumably you're
        using one that doesn't, probably something in the x86 family.
        >
        Yes. Or one that has a silent fixup in place, but that's rarer.

        - formerly david.thompson1 || achar(64) || worldnet.att.ne t

        Comment

        • Richard Tobin

          #5
          Re: bus error

          In article <78civ3d3jeur1m plnene9vmvi1988 27lca@4ax.com>,
          David Thompson <dave.thompson2 @verizon.netwro te:
          I read an article which claims that an invalid address ( not aligned
          >in power of two ) would cause a bus error exception, i tried the
          >following program and it didn't give any error,
          >You don't really mean "power of two". Processors that require
          >alignment usually require alignment on a multiple of the object size.
          >Technically, but the sizes of primitive types almost always are powers
          >of two: 2, 4, 8, or 16 bytes. (On byte machines, which nowadays are
          >all 8 bits.) This is the sort of alignment that actually allows
          >hardware advantages;
          I wasn't suggesting that there might be non-power-of-two alignment,
          but that you can't just use an arbitrary power of two; you have to
          use whatever is appropriate to the object in question.
          >multiples of 5, while formally similar, would in
          >practice be worthless and thus not used.
          Incidentally, 5 byte floats were common on some early microprocessor
          systems, but of course they had no alignment restrictions as they
          accessed memory in single bytes.

          -- Richard
          --
          :wq

          Comment

          Working...