A
aegis
The following was mentioned by Eric Sosman
from
http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source
"The alignment requirement for any type T must be a
divisor of sizeof(T). (Proof: In `T array[2];' both
`array[0]' and `array[1]' must be correctly aligned.)
Since `sizeof(unsigned char)' is divisible only by one
and since one is a divisor of every type's size, every
type is suitably aligned for `unsigned char'."
Now if on a system where sizeof(long *) == sizeof(int *)
the implication is that a pointer to int can hold any values
that a pointer to long can and vice versa, however this
does not necessarily mean dereferencing a pointer to
int will yield a valid value or dereferencing a pointer to long
will yield a valid value. Example:
long *p;
int *p1;
long value = LONG_MAX;
p = &value;
p1 = p;
*p1; /* possibly a trap representation -- correct? */
It is possible, for either, to yield a trap
representation given disparate sizes between long and int or
even through modifying the padding bits of an object of type int
(if sizeof (long) >= sizeof(int) and not all bits were used to
represent values in an int),
if such padding bits existed for said object type.
And the special case for pointer to object types is
pointer to void, pointer to char, pointer to unsigned char,
pointer to signed char.
These pointer types are correctly aligned to hold any pointer
value that represents the location of an object.
However, you may not necessarily expect valid results
when dereferencing any of those pointer types(pointer to void
excluded), because under c89/90 a pointer to char or
pointer to signed char can, through dereferencing,
invoke undefined behavior? I am not too sure here.
I know under c99 trap representations were introduced
along with the raising of an implementation defined signal.
So in the case of c99, it is clear that the latter two can
happen if you were to, say, do the following:
sizeof(long *) == sizeof(char *)
sizeof(char) < sizeof(long)
long *p;
char *p1;
long a = 10;
p1 = p = &a;
*p1; /*
* undefined behavior due to:
* a) trap representation
* b) implementation defined signal raised
*/
Is my explanation apt? What is the case under c89
being that concepts such as trap representation
do not exist under c89?
from
http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source
"The alignment requirement for any type T must be a
divisor of sizeof(T). (Proof: In `T array[2];' both
`array[0]' and `array[1]' must be correctly aligned.)
Since `sizeof(unsigned char)' is divisible only by one
and since one is a divisor of every type's size, every
type is suitably aligned for `unsigned char'."
Now if on a system where sizeof(long *) == sizeof(int *)
the implication is that a pointer to int can hold any values
that a pointer to long can and vice versa, however this
does not necessarily mean dereferencing a pointer to
int will yield a valid value or dereferencing a pointer to long
will yield a valid value. Example:
long *p;
int *p1;
long value = LONG_MAX;
p = &value;
p1 = p;
*p1; /* possibly a trap representation -- correct? */
It is possible, for either, to yield a trap
representation given disparate sizes between long and int or
even through modifying the padding bits of an object of type int
(if sizeof (long) >= sizeof(int) and not all bits were used to
represent values in an int),
if such padding bits existed for said object type.
And the special case for pointer to object types is
pointer to void, pointer to char, pointer to unsigned char,
pointer to signed char.
These pointer types are correctly aligned to hold any pointer
value that represents the location of an object.
However, you may not necessarily expect valid results
when dereferencing any of those pointer types(pointer to void
excluded), because under c89/90 a pointer to char or
pointer to signed char can, through dereferencing,
invoke undefined behavior? I am not too sure here.
I know under c99 trap representations were introduced
along with the raising of an implementation defined signal.
So in the case of c99, it is clear that the latter two can
happen if you were to, say, do the following:
sizeof(long *) == sizeof(char *)
sizeof(char) < sizeof(long)
long *p;
char *p1;
long a = 10;
p1 = p = &a;
*p1; /*
* undefined behavior due to:
* a) trap representation
* b) implementation defined signal raised
*/
Is my explanation apt? What is the case under c89
being that concepts such as trap representation
do not exist under c89?