[cfe-dev] RFC: Enforcing pointer type alignment in Clang

James Y Knight via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 14 15:57:08 PST 2016


Or, for a more straightforward example, why should this be allowed on a
value of "highlyAlignedStruct" which is not aligned properly for the
struct's type:
  char *member = &highlyAlignedStruct->charMember;
while this is not allowed, neither in C nor C++:
  char *member = &((someStruct*)0)->charMember;

Note that no memory is actually loaded in either case.

On Thu, Jan 14, 2016 at 6:43 PM, James Y Knight <jyknight at google.com> wrote:

> Are those rules the same as the rules for what you're allowed to do with
> null pointers? I think it would be pretty nice if the rules for what's
> allowable to do with a null pointer, and what's allowable to do with an
> unaligned pointer were the same rules. (Even if basically nobody can
> actually understand what the rules are...)
>
> E.g. apparently this is considered okay in C++, despite the apparent
> dereference of "foo". (from chat on #llvm, apparently it arguably isn't
> valid per the standard, but CWG232 calls that a defect, and it *is*
> explicitly valid in C, per C11 6.5.3.2 paragraph 3):
>   int *foo = 0;
>   int *bar = &*foo;
>
> So, in your rules, is this okay, or not:
>   int foo;
>   int *unaligned = (int*)(1 + (char*)(&foo));
>   int *bar = &*unaligned;
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160114/f97ca88c/attachment.html>


More information about the cfe-dev mailing list