Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Andy Walker Newsgroups: comp.compilers Subject: Re: Bounds checking, Optimization techniques and undefined behavior Date: Mon, 6 May 2019 14:40:10 +0100 Organization: Not very much Lines: 40 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-05-039@comp.compilers> References: <19-04-021@comp.compilers> <19-04-023@comp.compilers> <19-04-037@comp.compilers> <19-04-039@comp.compilers> <19-04-042@comp.compilers> <19-04-044@comp.compilers> <19-04-047@comp.compilers> <19-05-004@comp.compilers> <19-05-006@comp.compilers> <19-05-016@comp.compilers> <19-05-020@comp.compilers> <19-05-024@comp.compilers> <19-05-025@comp.compilers> <19-05-028@comp.compilers> <19-05-032@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="37796"; mail-complaints-to="abuse@iecc.com" Keywords: C, standards Posted-Date: 06 May 2019 10:51:11 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Content-Language: en-GB Xref: csiph.com comp.compilers:2274 On 06/05/2019 01:15, our esteemed moderator wrote: > [In the struct { int a,b,c,d; } S example it is my understanding that &S and &S.a > have to be the same, Not "the same", as they have different types; but yes, they must compare equal. > the four ints have to be in the order declared, Yes, and they must be "sequentially allocated". Whether that means the same as "contiguously allocated" [like array members] is not entirely clear, as the C Standard doesn't define these terms. Structures can contain padding, in general, but [AFAICT] not in a way that affects this debate. At least it seems to be the case that &S.a+1 must compare equal to &S.b, see N1570, section 6.5.9 para 6, and footnote 109; but I can't quite make even this case watertight. > and they > all have the same alignment so it is valid abeit poor form to treat them as > an array. -John] For the purposes of pointer arithmetic [and therefore arrays] and comparison, non-array objects are treated as arrays with a single element. So &S+0 and &S+1 are valid, though the latter can't be dereferenced; likewise &S.a+0 and &S.a+1, and they can be compared with &S.b, etc. But I can see no support in the Standard for &S.a+2 being anything other than UB; it points neither to "the" element of S.a, nor one past. That's even though [or if!] &S.a+1 == &S.b and &S.b+1 == &S.c; Section 6.5.6, paras 8 and 9. As George Neuner says, if you want to use S also as an array, then you should have declared it as a union. In Real Life, you will surely get away with such things, and with much technically undefined pointer arithmetic that temporarily strays outside an array [without dereference]. It's sanctioned by dubious custom, but not by the Standard. [Yes, I've done it myself. I'm suitably ashamed and am hereby rapped on the knuckles.] -- Andy Walker, Nottingham.