Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Bart Newsgroups: comp.compilers Subject: Re: Bounds checking, Optimization techniques and undefined behavior Date: Wed, 8 May 2019 14:37:31 +0100 Organization: virginmedia.com Lines: 30 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-05-067@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-029@comp.compilers> <19-05-034@comp.compilers> <19-05-045@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="57482"; mail-complaints-to="abuse@iecc.com" Keywords: C, design Posted-Date: 08 May 2019 12:46:45 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:2302 On 07/05/2019 10:04, David Brown wrote: > On 06/05/2019 10:15, Hans-Peter Diettrich wrote: >> I learned to love the Pascal indexing instead of pointers, because a >> loop like >>   for i := 1 to 10 do sum := sum + A[i]; >> can be optimized safely by the Pascal/Delphi compiler into pointer and >> auto increment, so that no speed penalty exists vs. explicit pointer >> usage. But in a C for loop the index variable can be changed in code, so >> that even above code would execute slower with bounds checks. >> > > It will not be slower in C - because the compiler knows that "i" is > never changed in the loop. (I'd like some way to have that enforced in > C, but I don't know of any good method.) Making loop variables declared in a for-header be implicitly 'const' could be done: for (int i=0; i