Re: Bounds checking, Optimization techniques and undefined behavior

David Brown <david.brown@hesbynett.no>
Wed, 8 May 2019 10:03:08 +0200

          From comp.compilers

Related articles
[26 earlier articles]
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)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior derek@_NOSPAM_knosof.co.uk (Derek M. Jones) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-09)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-09)
[2 later articles]
| List of all articles for this month |

From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.compilers
Date: Wed, 8 May 2019 10:03:08 +0200
Organization: A noiseless patient Spider
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-029 19-05-034 19-05-045
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="52666"; mail-complaints-to="abuse@iecc.com"
Keywords: C, optimize, comment
Posted-Date: 08 May 2019 12:33:40 EDT
Content-Language: en-GB

On 07/05/2019 11:04, David Brown wrote:
> [For the loop with the unchangable index, you can declare the index
> const and cast the places you change it, but ugh.
>
> const int i;
>
> for(*(int*)&i = 0; i < 10; (*(int*)&i)++) {
> a[i] = i;
> i++; /* will be diagnosed as an error */
> }
> -John]
>


Looking more closely at your code here, it is wrong.


You can cast away "const" from something that was originally declared
without "const" and had "const" added via a cast. You cannot cast away
"const" from something declared "const". The compiler is free to assume
that "i" never changes, and ignore (or complain about) your attempts to
change it via the casts.


There is a much better way, but it is still ugly:


for (int i = 0; i < 10; i++) {
const int ii = i; const int i = ii;


a[i] = 1;
i++; // Error
}


You can, of course, just use a new local const variable, but then you
lose the matching names.




In C++, this won't work because the first "int i" scope is /inside/ the
compound statement, while in C it is /before/ that statement. In C++,
you need:


for (int i = 0; i < 10; i++) {{
const int ii = i; const int i = ii;


a[i] = 1;
i++; // Error
}}




So it can be done, but it would be nicer if it were better integrated in
the language. For a new language, I would make such immutability part
of the definition of the "for" loop.


[I'm looking at C11 and don't see where it says you can't cast away
const. What am I missing? I agree any approach here is ugly and we're
near the limits of what you can say in C. -John]


Post a followup to this message

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