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


Groups > comp.lang.c > #387694

Re: size_t best practice

Path csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Ike Naar <ike@sdf.org>
Newsgroups comp.lang.c
Subject Re: size_t best practice
Date Thu, 22 Aug 2024 11:19:24 -0000 (UTC)
Organization A noiseless patient Spider
Lines 21
Message-ID <slrnvce7ls.l93.ike@iceland.freeshell.org> (permalink)
References <VdCcne2MOeshN1z7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <va275j$3d6tp$1@dont-email.me> <86ed6h9cxm.fsf@linuxsc.com>
Injection-Date Thu, 22 Aug 2024 13:19:25 +0200 (CEST)
Injection-Info dont-email.me; posting-host="3cf8d2f740ec50d350419ecde0d54117"; logging-data="452470"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TmK/7k23OcLDEmbnrQeLU"
User-Agent slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
Cancel-Lock sha1:ijBlY6oPOzx3VQ1AGJanGVYCaP0=
Xref csiph.com comp.lang.c:387694

Show key headers only | View raw


On 2024-08-22, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
>
>> We can immediately apply the pattern I demonstrated above to this
>> and get
>>
>>   for (size_t i = v->_size - 1; i != index - 1; --i)
>>     v->_values[i + 1] = v->_values[i];
>>
>> Done.  No need for an extra safeguard.
>
> Better (please ignore cosmetic layout differences):
>
>     for( size_t i = v->_size;  i > index;  i--  ){
>         v->_values[i] = v->_values[i-1];
>     }

Or even get rid of the for loop, and use memmove() :

  memmove(v->_values + index + 1, v->_values + index,
          (v->_size - index) * sizeof *v->_values);

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