Re: Bounds checking, Optimization techniques and undefined behavior

Andy Walker <anw@cuboid.co.uk>
Mon, 6 May 2019 14:40:10 +0100

          From comp.compilers

Related articles
[16 earlier articles]
Re: Bounds checking, Optimization techniques and undefined behavior gneuner2@comcast.net (George Neuner) (2019-05-05)
Re: Bounds checking, Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior DrDiettrich1@netscape.net (Hans-Peter Diettrich) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior christopher.f.clark@compiler-resources.com (Christopher F Clark) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior 0xe2.0x9a.0x9b@gmail.com (Jan Ziak) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-06)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Bounds checking, Optimization techniques and undefined behavior nuno.lopes@ist.utl.pt (Nuno Lopes) (2019-05-07)
[12 later articles]
| List of all articles for this month |

From: Andy Walker <anw@cuboid.co.uk>
Newsgroups: comp.compilers
Date: Mon, 6 May 2019 14:40:10 +0100
Organization: Not very much
References: 19-04-021 19-04-023 19-04-037 19-04-039 19-04-042 19-04-044 19-04-047 19-05-004 19-05-006 19-05-016 19-05-020 19-05-024 19-05-025 19-05-028 19-05-032
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
Content-Language: en-GB

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.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.