Word alignment - Why doesn't this crash?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ollemblomgren@gmail.com

    Word alignment - Why doesn't this crash?

    Hi,

    I try to confirm my understanding of word alignment by writing a
    program that screw this particular matter up.

    I run linux on:
    Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz

    my compiler is:
    gcc version 4.1.2

    I compile with no arguments other than source file name and target
    name (-o ptr_test).

    I write this program but it doesn't crash. Is it because I'm testing
    the wrong thing (my understanding is wrong) or is it because my
    environment is very forgiving?

    #include <stdlib.h>
    #include <stdio.h>

    int main( int argc, char** argv )
    {
    char* ptr = malloc( 32 );

    ptr++;

    *(int*)ptr = 20;

    printf("ptr: %d\n", *ptr );

    *(int*)ptr = *(int*)ptr + 20;


    printf("ptr: %d\n", *ptr );

    return(0);
    }


    Thanks
    /Sune
  • Philip Potter

    #2
    Re: Word alignment - Why doesn't this crash?

    ollemblomgren@g mail.com wrote:
    I try to confirm my understanding of word alignment by writing a
    program that screw this particular matter up.
    >
    I run linux on:
    Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz
    >
    my compiler is:
    gcc version 4.1.2
    >
    I compile with no arguments other than source file name and target
    name (-o ptr_test).
    >
    I write this program but it doesn't crash. Is it because I'm testing
    the wrong thing (my understanding is wrong) or is it because my
    environment is very forgiving?
    Your program exhibits undefined behaviour. The answer to FAQ 11.35 is
    relevant (and much better written than anything I write):

    A compiler may do anything it likes when faced with undefined
    behavior (and, within limits, with implementation-defined and
    unspecified behavior), including doing what you expect. It's unwise
    to depend on it, though.

    Here is another way of looking at it, due to Roger Miller:

    ``Somebody told me that in basketball you can't hold the ball and
    run. I got a basketball and tried it and it worked just fine. He
    obviously didn't understand basketball.''

    It seems your implementation doesn't mind that particular alignment
    issue. C doesn't say that alignment issues /will/ happen, only that they
    /might/.

    Philip

    Comment

    • Andrey Tarasevich

      #3
      Re: Word alignment - Why doesn't this crash?

      ollemblomgren@g mail.com wrote:
      >
      I write this program but it doesn't crash. Is it because I'm testing
      the wrong thing (my understanding is wrong) or is it because my
      environment is very forgiving?
      >
      There's no such thing as some mandatory "word alignment". Alignment
      requirements are type-specific and platform-specific. It is quite
      possible that in your platform objects of type 'int' can be placed in
      memory in any way you want, i.e. there are no alignment restrictions as all.

      Note also, that the real "platform" you are working with is really the
      platform provided (emulated) by the compiler. It doesn't necessarily
      have to inherit all (or any) requirements from the underlying hardware
      platform. Normally it does for performance reasons, but it is not
      impossible to implement a C compiler that wouldn't impose any alignment
      restrictions on any of its data types, even though the underlying
      hardware platform does have such restriction for "correspond ing"
      hardware data types.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Sune

        #4
        Re: Word alignment - Why doesn't this crash?

        On 1 Apr, 03:53, Andrey Tarasevich <andreytarasev. ..@hotmail.com>
        wrote:
        ollemblomg...@g mail.com wrote:
        >
        I write this program but it doesn't crash. Is it because I'm testing
        the wrong thing (my understanding is wrong) or is it because my
        environment is very forgiving?
        >
        There's no such thing as some mandatory "word alignment". Alignment
        requirements are type-specific and platform-specific. It is quite
        possible that in your platform objects of type 'int' can be placed in
        memory in any way you want, i.e. there are no alignment restrictions as all.
        >
        Note also, that the real "platform" you are working with is really the
        platform provided (emulated) by the compiler. It doesn't necessarily
        have to inherit all (or any) requirements from the underlying hardware
        platform. Normally it does for performance reasons, but it is not
        impossible to implement a C compiler that wouldn't impose any alignment
        restrictions on any of its data types, even though the underlying
        hardware platform does have such restriction for "correspond ing"
        hardware data types.
        >
        --
        Best regards,
        Andrey Tarasevich
        Hi,

        ok your answer set my mind straight. C impose very little requirements
        and governance when it comes to word alignment. It ultimately depends
        on the compiler.

        Thanks
        /Olle

        Comment

        • Stephen Sprunk

          #5
          Re: Word alignment - Why doesn't this crash?

          <ollemblomgren@ gmail.comwrote in message
          news:c2f855b9-fe79-4ac0-8bcd-9b7d68896938@c1 9g2000prf.googl egroups.com...
          I try to confirm my understanding of word alignment by writing a
          program that screw this particular matter up.
          >
          I run linux on:
          Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz
          >
          my compiler is:
          gcc version 4.1.2
          >
          I compile with no arguments other than source file name and target
          name (-o ptr_test).
          >
          I write this program but it doesn't crash. Is it because I'm testing
          the wrong thing (my understanding is wrong) or is it because my
          environment is very forgiving?
          There is no _requirement_ that your program crash when you perform an
          unaligned access. That is merely one possible result.

          x86 happens to be rather forgiving in that respect: unaligned accesses can
          be significantly slower, but they do not cause a crash.

          S

          --
          Stephen Sprunk "God does not play dice." --Albert Einstein
          CCIE #3723 "God is an inveterate gambler, and He throws the
          K5SSS dice at every possible opportunity." --Stephen Hawking

          Comment

          Working...