[cfe-dev] sizeof (RopeRefCountString)

Ted Kremenek kremenek at apple.com
Mon Sep 15 21:43:43 PDT 2008


On Sep 15, 2008, at 9:39 PM, Eli Friedman wrote:

> On Mon, Sep 15, 2008 at 9:22 PM, Ted Kremenek <kremenek at apple.com>  
> wrote:
>>
>> On Sep 15, 2008, at 6:35 PM, Zhongxing Xu wrote:
>>
>>
>> 2008/9/16 Ted Kremenek <kremenek at apple.com>
>>>
>>> On Sep 7, 2008, at 1:32 AM, Zhongxing Xu wrote:
>>>
>>>> Hi,
>>>>
>>>> In RewriteRope.cpp, RewriteRope::MakeRopeString(const char  
>>>> *Start, const
>>>> char *End),
>>>> we calculate the AllocSize by:
>>>>
>>>> unsigned AllocSize = sizeof(RopeRefCountString) - 1 +  
>>>> AllocChunkSize;
>>>>
>>>> I guess here the intention is: sizeof(RopeRefCountString) is 5.  
>>>> But gcc
>>>> says sizeof(RopeRefCountString) is 8. So the actual AllocSize is  
>>>> 4087.
>>>> Should we minus 4 instead of 1 to make the AllocSize 4084?
>>>
>>> The "-1" I believe is to accommodate for the field Data[1], which  
>>> occupies
>>> a single byte:
>>
>> In practice it occupies 4 bytes because of tail padding.
>>
>> Right.  Isn't there a pointer arithmetic trick we can do here to  
>> get the
>> right number?
>
> "offsetof(RopeRefCountString, Data)"?

Thanks Eli.

Using offsetof, probably the number we want is:

   offsetof(RopeRefCountString, Data) + AllocChunkSize





More information about the cfe-dev mailing list