public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: "Maciej W. Rozycki" <macro@orcam.me.uk>
Cc: Alejandro Colomar <alx.manpages@gmail.com>,
	A <amit234234234234@gmail.com>,
	libc-alpha@sourceware.org
Subject: Re: size_t vs long.
Date: Thu, 17 Nov 2022 18:47:14 -0800	[thread overview]
Message-ID: <45e874b3-315f-a24b-0a4c-3c0d6d2955b3@cs.ucla.edu> (raw)
In-Reply-To: <alpine.DEB.2.21.2211180156370.54611@angie.orcam.me.uk>

On 11/17/22 18:11, Maciej W. Rozycki wrote:

>>> for (size_t i = 41; i < sizeof A / sizeof A[0]; --i) {
>>>     A[i] = something_nice;
>>> }

>  ... what exactly is bogus with the first loop?
> 
>   AFAICT if index 41 is within the bounds of A, it fills elements [0..41]
> with something_nice and otherwise it does nothing.

Yes, and that's precisely what is bogus about it. Most people who read 
that code won't easily see that you're summarizing it correctly 
(assuming INT_MAX < SIZE_MAX). And if I saw that code in a real program, 
my first guess - and it most likely would be the correct guess - is that 
the *author* of the code didn't know what it does, it's so confusingly 
written.

Certainly Alejandro was confused by that bogus loop, as his most recent 
email said the following:

> For code that means exactly the same as
> 
> for (size_t i = 41; i < nitems(A); --i) {
>     A[i] = something_nice;
> }
> 
> 
> we need:
> 
> 
> for (size_t i = 0; i < nitems(A) && i <= 41; ++i) {
>     A[i] = something_nice;
> }
> 
> or
> 
> for (idx_t i = 0; i < nitems(A) && i <= 41; ++i) {
>     A[i] = something_nice;
> }
> 
> or
> 
> for (idx_t i = 41; i < nitems(A) && i > 0; --i) {
>     A[i] = something_nice;
> }
> 
> (always assuming SIZE_MAX > INT_MAX.) 

and if we call those four loops A, B, C and D, then Alejandro was 
incorrect about B and C because they are not equivalent to A. And 
although D is equivalent to A, D is still confusing and is still Bad Code.

Code like this should be written more the way you said it. E.g.:

    idx_t nice_count = 42;
    if (nice_count <= nitems(A))
      for (idx_t i = 0; i < nice_count; i++)
        A[i] = something_nice;

This is much easier to understand than the other alternatives given so 
far, for reasons that I hope are obvious. And as a bonus, it doesn't 
assume INT_MAX < SIZE_MAX.

  reply	other threads:[~2022-11-18  2:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17  7:02 A
2022-11-17  9:21 ` Alejandro Colomar
2022-11-17  9:48   ` A
2022-11-17 11:00     ` Alejandro Colomar
2022-11-17 19:40       ` Jason Duerstock
2022-11-17 20:01         ` Alejandro Colomar
2022-11-17 19:17   ` Paul Eggert
2022-11-17 20:27     ` Alejandro Colomar
2022-11-17 21:39       ` Paul Eggert
2022-11-17 23:04         ` Alejandro Colomar
2022-11-23 20:08           ` Using size_t to crash on off-by-one errors (was: size_t vs long.) Alejandro Colomar
2022-11-18  2:11         ` size_t vs long Maciej W. Rozycki
2022-11-18  2:47           ` Paul Eggert [this message]
2022-11-23 20:01             ` Alejandro Colomar
2022-11-17 21:58 ` DJ Delorie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45e874b3-315f-a24b-0a4c-3c0d6d2955b3@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=alx.manpages@gmail.com \
    --cc=amit234234234234@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=macro@orcam.me.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).