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



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

.



Relevant Pages

  • Re: Isnt it time there was a standard align statement?
    ... C language actually allowed some portable control over data ... /* per member alignment (obviously padding before the first ... fatal with compiler specific switches - and that's no business of the ...
    (comp.lang.c)
  • Re: How does C cope if architecture doesnt address bytes?
    ... >>which the malloc() call occurs, figure out the type of the pointer which ... alignment in the implementation, rather than in either part of it. ... compiler and library are so strictly separate that there is no ... # The pointer returned if the allocation succeeds is suitably aligned so ...
    (comp.lang.c)
  • Re: register attribute
    ... The point of dynamic data structures is that the mass of ... I recall the alignment problem being reported in Byte ... > the programmer will know but the compiler won't. ...
    (comp.lang.fortran)
  • Re: Isnt it time there was a standard align statement?
    ... same method of specifying data alignment. ... The hardware is the same, ... fatal with compiler specific switches - and that's no business of the ... A vector math portability library. ...
    (comp.lang.c)
  • Re: Overloading class new and delete operators with parameters
    ... "Tom Widmer" wrote in message ... Shifting alignment by 4 bytes might slow down ... it's a class member the compiler can work out any alignment issues. ... Sure it can mark what bytes it knows ...
    (microsoft.public.vc.language)