[Openais] [lck 2/7] Sizeof should be structure and not pointer

Jim Meyering jim at meyering.net
Tue Jun 2 11:07:51 PDT 2009


Steven Dake wrote:
> On Tue, 2009-06-02 at 18:11 +0200, Jim Meyering wrote:
>> Ryan O'Hara wrote:
>> > This patch is correct, but what I intended was to use sizeof (struct
>> > resource_instance). Can we do that instead?
>>
>> Please try to move to "sizeof (VARIABLE)".
>
> I really don't prefer this because people __often__ make the error of
> not dereferencing a pointer type.  This happens more often then a
> structure completely changes on the variable the sizeof should operate
> on.  These sorts of errors are extremely difficult to notice as well.

Whichever way you do it, there's risk of getting it wrong.
It's mainly a question of getting used to an idiom and
knowing what to watch out for:

For example, I know right away that these are correct,
assuming that they compile (i.e., no context required):

    p = malloc (sizeof *p);
    ...
    new_q = realloc (q, sizeof *p);


On the other hand, if I use explicit type names,

    p = malloc (sizeof (struct foo));
    ...
    new_q = realloc (q, sizeof (struct foo *));

Then I have to find each declaration and ensure that the type matches.


If I audit the code for this and find two or more errors,
would you consider a change of policy?


More information about the Openais mailing list