Deutsch English Français Italiano |
<vvqkf9$hp7u$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Loops (was Re: do { quit; } else { }) Date: Sun, 11 May 2025 18:49:44 +0200 Organization: A noiseless patient Spider Lines: 39 Message-ID: <vvqkf9$hp7u$1@dont-email.me> References: <vspbjh$8dvd$1@dont-email.me> <vtc19j$2kqlj$1@dont-email.me> <87a58mqt2o.fsf@nosuchdomain.example.com> <vtc7mp$2q5hr$1@dont-email.me> <vtcqf6$3j95s$1@dont-email.me> <vtdh4q$b3kt$1@dont-email.me> <vtf7fe$1qtpg$1@dont-email.me> <vtgfuf$31ug1$1@dont-email.me> <20250413072027.219@kylheku.com> <vtgpce$39229$1@dont-email.me> <vti2ki$g23v$1@dont-email.me> <vtin99$vu24$1@dont-email.me> <vtiuf0$18au8$1@dont-email.me> <vtj97r$1i3v3$1@dont-email.me> <vtl166$36p6b$1@dont-email.me> <vtlcg0$3f46a$2@dont-email.me> <20250415153419.00004cf7@yahoo.com> <86h62078i8.fsf@linuxsc.com> <20250504180833.00000906@yahoo.com> <86plggzilx.fsf@linuxsc.com> <vvnsvt$3k1mu$1@dont-email.me> <86ldr4yx0x.fsf@linuxsc.com> <vvpmm2$3dhl$1@dont-email.me> <vvpsji$4jht$1@dont-email.me> <vvqfpp$gr89$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 May 2025 18:49:52 +0200 (CEST) Injection-Info: dont-email.me; posting-host="8d73a98b3ec8e761f81047da6c4b10e4"; logging-data="582910"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19koa1pjxYgeaGUpdNNpuik8XwheIGAfN8=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:erTPJVXdT8nKDI0FvyxppdqquNA= In-Reply-To: <vvqfpp$gr89$1@dont-email.me> Content-Language: en-GB On 11/05/2025 17:30, Muttley@dastardlyhq.com wrote: > On Sun, 11 May 2025 12:02:25 +0200 > David Brown <david.brown@hesbynett.no> gabbled: >> double average(double values[static 4]) { >> double total = 0.0; >> for (int i = 0; i < 3; i++) { >> total += values[i]; >> } >> return total / 3; >> } >> >> Without the "static 4", the compiler can only assume that it can >> access up to 3 doubles from the "value" array. But with "static 4", >> it knows it is safe to read "values[3]" even though the code never >> needs to do > > Not sure I follow. C doesn't care if its safe or not, it'll just try and > read them anyway and if it can't and there's a memory leak or crash, > well, tough luck mate. So I don't see why it would make a difference to > the resulting > assembler. > > No, C (and the C compiler) /does/ care if it something is safe or not. But it believes the programmer if the programmer says it is safe. Thus if the programmer writes code that accesses "values[3]", the compiler may assume it is safe to do so at other times too. But if the programmer never accesses beyond "values[2]", the compiler cannot assume it is safe to access "values[3]". If you want the compiler to be able to generate code that is more efficient by accessing "values[4]", you have to tell it that it is safe to do so. And using "static 4" here in the parameter is one way of saying that. There are other ways that could be used - if you write "(void) values[3];", the compiler could see that it is safe to read that address. Whether or not a given compiler optimiser would use that information is another matter, of course.