Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently

Michael Kerrisk (man-pages)
2020-09-05T08:27:56Z

Hi Alex,

On 9/4/20 5:12 PM, Alejandro Colomar wrote:
> Hi Michael,
>
> Sorry for the deep threading, I noticed it late and I tried to fix it in
> the last messages. Also there's some email where I forgot to remove
> "Re:" from the subject.

Yes, the threading made things a little tricky, especially when it
came to trying to review what I'd done. Did you not send with
"git send-email"? Usually that threads things nicely (all patches
after the first as replies to the first patch).

> Well, that's all! I can see 2 or 3 patches where I have doubts, but I
> think most of them are straightforward.

So, I've still not processed patches 21, 22, and 29. And in review,
I see that I am wondering about whether I should maintain 1, 5, 17,
18, and 19. These all involve the use of malloc() or similar.

The existing pattern was something like:

struct mytype *x; // Or some simple type such as 'int'
...
x = malloc(n * sizeof(struct mytpe));

and your patches change it to:

struct mytype *x;
...
x = malloc(n * sizeof(*x));

I'm not sure that always helps readability.

Part of the problem is the use of C90 in the code.

Do you both agree with me that both of the following c99
forms are better than the original:

struct mytype *x = malloc(n * sizeof(struct mytpe));
struct mytype *x = malloc(n * sizeof(*x));

?

I *think* I mildly prefer the first form, but I'm open to
arguments that the latter form is preferable. Of course, the
fact that there might be more than one point where an 'alloc'
is done and assigned to 'x' may influence the argument. Thus

struct mytype *x = malloc(n * sizeof(struct mytpe));
...
x = malloc(p * sizeof(struct mytype));

vs

struct mytype *x = malloc(n * sizeof(*x));
...
x = malloc(p * sizeof(*x));

Thanks,

Michael

--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/