Word alignment - Why doesn't this crash?

O

ollemblomgren

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
 
P

Philip Potter

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
 
A

Andrey Tarasevich

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 "corresponding"
hardware data types.
 
S

Sune

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 "corresponding"
hardware data types.

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
 
S

Stephen Sprunk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top