J
junky_fellow
K&R say that,
It is guaranteed that 1) a pointer to an object may be converted to a
pointer to an object whose type requires less
or equally strict storage alignment and 2) back again without change.
My question is that, is it legal to dereference the intermediate
pointer (the pointer with less strict alignment that we get after
conversion) ? Does this intermediate pointer also point to the
same memory location as the pointer (with more strict storage
alignment) from which it was converted, of course, the type of
object pointed would be different.
For eg. Consider the following piece of code:
int main(void)
{
int arr[10];
int *i_ptr; /* has more strict storage alignment */
short *s_ptr; /* has less strict storage alignment as
compared to i_ptr */
i_ptr = arr + 4;
s_ptr = (short *)i_ptr; /* should be valid, since s_ptr has
less strict storage alignment
as compared to i_ptr */
}
Now, is this legal to deference s_ptr ?
Does s_ptr also point to the same memory location as i_ptr ? (of
course the type of objects they point to are different, and
dereferencing "s_ptr" may give different results on machines that
have different endianness )
I am asking this question because I think that "short *" might have
an entirely different representation as compared to "int *".
So, during converison does the complier take care of this so that
the "short *" obtained after convesrsion is perfectly valid and
it also points to the same meory location ?
It is guaranteed that 1) a pointer to an object may be converted to a
pointer to an object whose type requires less
or equally strict storage alignment and 2) back again without change.
My question is that, is it legal to dereference the intermediate
pointer (the pointer with less strict alignment that we get after
conversion) ? Does this intermediate pointer also point to the
same memory location as the pointer (with more strict storage
alignment) from which it was converted, of course, the type of
object pointed would be different.
For eg. Consider the following piece of code:
int main(void)
{
int arr[10];
int *i_ptr; /* has more strict storage alignment */
short *s_ptr; /* has less strict storage alignment as
compared to i_ptr */
i_ptr = arr + 4;
s_ptr = (short *)i_ptr; /* should be valid, since s_ptr has
less strict storage alignment
as compared to i_ptr */
}
Now, is this legal to deference s_ptr ?
Does s_ptr also point to the same memory location as i_ptr ? (of
course the type of objects they point to are different, and
dereferencing "s_ptr" may give different results on machines that
have different endianness )
I am asking this question because I think that "short *" might have
an entirely different representation as compared to "int *".
So, during converison does the complier take care of this so that
the "short *" obtained after convesrsion is perfectly valid and
it also points to the same meory location ?