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

Jim Meyering jim at meyering.net
Tue Jun 2 12:27:27 PDT 2009


Steven Dake wrote:

> On Tue, 2009-06-02 at 20:07 +0200, Jim Meyering wrote:
>> 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);

Oops.  For the record, that should be

       new_q = realloc (q, sizeof *q);

>> 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 declarat.ion 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?
>
> I don't have a strong preference as long as the code base is
> consistently using one style or the other.  We can closely watch those
> commits on sizeofs to make sure they operate on the full type instead of
> the pointer type.

Ok.
I'll see about automating the check and/or conversion.


More information about the Openais mailing list