Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connectionsPath: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown
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:
References:
<87a58mqt2o.fsf@nosuchdomain.example.com>
<20250413072027.219@kylheku.com>
<20250415153419.00004cf7@yahoo.com> <86h62078i8.fsf@linuxsc.com>
<20250504180833.00000906@yahoo.com> <86plggzilx.fsf@linuxsc.com>
<86ldr4yx0x.fsf@linuxsc.com>
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:
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 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.