<div dir="ltr">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:<div><div><div> char *member = &highlyAlignedStruct->charMember;</div></div><div>while this is not allowed, neither in C nor C++:</div><div><div> char *member = &((someStruct*)0)->charMember;</div></div></div><div><br>Note that no memory is actually loaded in either case.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 14, 2016 at 6:43 PM, James Y Knight <span dir="ltr"><<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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...)<div><div><div><br></div><div>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):</div><div> int *foo = 0;</div><div><div> int *bar = &*foo;</div></div><div><br></div><div>So, in your rules, is this okay, or not:</div></div><div> int foo;</div><div> int *unaligned = (int*)(1 + (char*)(&foo));</div><div> int *bar = &*unaligned;</div><div><br></div></div></div>
</blockquote></div><br></div>