Re: [PATCH] builtin/mv.c: use correct type to compute size of an array element
From: Junio C Hamano <hidden>
Date: 2022-07-07 20:25:41
Jeff King [off-list ref] writes:
On Wed, Jul 06, 2022 at 07:02:18PM -0700, Junio C Hamano wrote:quoted
* Should we in general use sizeof(TYPE) in these cases, instead of the size of the zeroth element, e.g. memmove(source + i, source + i + 1, n * sizeof(source[i])); It would have been caught by the above Coccinelle rule (we are taking the size of *dst).I'm not sure I understand this. As you noted in a later email, using sizeof(TYPE) is less maintainable if the type of "source" changes.
Sorry for a typo or thinko, whichever one you like ;-)
But later you mention using "*source" instead of "source[i]". I don't think there is a particular reason to prefer one over the other from the compiler perspective. I find "*source" more idiomatic (but better still of course is MOVE_ARRAY, which removes the choice entirely).
Yes, I wrote source[i] there only because I found it somewhat awkward to write source[0] or *source there, when the moved (sub)array is from the index 'i' to the end. *(source + i) would have matched the intention better but it still is awkward.