Sujet : Re: size_t best practice
De : ike (at) *nospam* sdf.org (Ike Naar)
Groupes : comp.lang.cDate : 22. Aug 2024, 12:19:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <slrnvce7ls.l93.ike@iceland.freeshell.org>
References : 1 2 3
User-Agent : slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
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);