On 09.07.2018 10:09, Martin Husemann wrote: > On Sun, Jul 08, 2018 at 03:30:36PM +0200, Kamil Rytarowski wrote: >> Misaligned pointer is explicitly documented as undefined behavior in the >> C standard (C11 6.3.2.3 p7). (In C++ it's basically the same.) > > Yes, but the standard dos not define what a misaligned pointer is > (or "correctly aligned"). > > It talks about the prefered alignment that implementations select and > that you can query with _Alignof - but there is no connection of that > value to the section you refer. Some implementations require minimal > alignement like their _Alignof returns, for others "correctly aligned" is > always true. > > I might be missing something though. > > Martin >
According to my understanding, alignment requirement for a type/object
is implementation defined (6.2.8); however during the process of
converting types, if the returned pointer is not correctly aligned the
result is undefined behavior (6.3.2.3 p7).
int16_t (short for LP64) and int8_t (char) have different alignment
requirements on NetBSD/amd64. char has the weakest one (6.2.8.6).
$ cat test.c
#include <stdio.h>
int
main(int argc, char **argv)
{
printf("alignof(char)=%zu\n", __alignof__(char));
printf("alignof(short)=%zu\n", __alignof__(short));
return 0;
}
$ ./a.out
alignof(char)=1
alignof(short)=2
If one alignment is larger than the other one, it's called stricter one
(6.2.8.7).
This means that mpbios_page (int8_t*) has weaker alignment than memtop
(int16_t*) and this cast is undefined behavior, because the returned
pointer is not correctly aligned for the destination type (as
&mpbios_page[0x413] violates it).
signature.asc
Description: OpenPGP digital signature
