comp.lang.c
[Top] [All Lists]

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

Subject: Re: Word alignment - Why doesn't this crash?
From: Philip Potter
Date: Mon, 31 Mar 2008 12:34:43 +0100
Newsgroups: comp.lang.c


ollemblomgren@xxxxxxxxx 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


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy