Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387683

Re: size_t best practice

Path csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: size_t best practice
Date Thu, 22 Aug 2024 01:38:24 -0700
Organization A noiseless patient Spider
Lines 33
Message-ID <86a5h59clb.fsf@linuxsc.com> (permalink)
References <VdCcne2MOeshN1z7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <va275j$3d6tp$1@dont-email.me> <va278t$3d70r$1@dont-email.me> <va27bc$3d6tp$2@dont-email.me>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Date Thu, 22 Aug 2024 10:38:25 +0200 (CEST)
Injection-Info dont-email.me; posting-host="dd674af37156d5461d600da65dab9de0"; logging-data="405193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fTBexG3ZH/OKB5NU4eRj/k9fiXv19uWg="
User-Agent Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock sha1:r9FtTH8rTpBoXVGyQFtTuAfiAlQ= sha1:+L8y3c6I0coIc7jIJtC+DGdmHJ4=
Xref csiph.com comp.lang.c:387683

Show key headers only | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:

> On 08/20/24 6:55 AM, Andrey Tarasevich wrote:
>
>> On 08/20/24 6:53 AM, Andrey Tarasevich wrote:
>>
>>> For example, iteration all the way to 0 can be idiomatically
>>> implemented as
>>>
>>>  for (some_unsigned_type i = size; (some_unsigned_type) i != -1; --i)
>>>  ...
>>
>> Sorry, a typo.  Was meant to be
>>
>>   for (some_unsigned_type i = size; i != (some_unsigned_type) -1; --i)
>>   ...
>
> Of, crap.  One more time
>
>   for (some_unsigned_type i = size - 1;
>        i != (some_unsigned_type) -1;
>        --i)
>     ...
>
> Should be good now :)

To me the redundant cast is a red flag.  A cleaner alternative:

   for( some_unsigned_type i = size;  i > 0 && i--;  ){
      ...
   }

Produces identical code to the above at -O1 (both gcc and clang).

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

size_t best practice Mark Summerfield <mark@qtrac.eu> - 2024-08-18 08:03 +0000
  Re: size_t best practice Ike Naar <ike@sdf.org> - 2024-08-18 08:38 +0000
    Re: size_t best practice Mark Summerfield <mark@qtrac.eu> - 2024-08-18 10:15 +0000
      Re: size_t best practice Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-08-20 07:38 -0700
  Re: size_t best practice Michael S <already5chosen@yahoo.com> - 2024-08-18 12:36 +0300
  Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-18 04:32 -0700
  Re: size_t best practice Michael S <already5chosen@yahoo.com> - 2024-08-18 15:40 +0300
    Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-18 15:23 -0700
      Re: size_t best practice Michael S <already5chosen@yahoo.com> - 2024-08-19 11:13 +0300
        Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-19 09:43 -0700
  Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-18 14:57 -0700
  Re: size_t best practice Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-08-20 06:53 -0700
    Re: size_t best practice Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-08-20 06:55 -0700
      Re: size_t best practice Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-08-20 06:56 -0700
        Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-22 01:38 -0700
    Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-22 01:31 -0700
      Re: size_t best practice Ike Naar <ike@sdf.org> - 2024-08-22 11:19 +0000
        Re: size_t best practice Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-22 06:12 -0700
  Re: size_t best practice Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-24 19:49 +0200
    Re: size_t best practice Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2024-08-25 06:30 +0200
      Re: size_t best practice Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-25 06:53 +0200
        Re: size_t best practice Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-26 21:39 +0100
  Re: size_t best practice Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-27 18:11 +0200

csiph-web