Ian Collins
5/20/2011 9:51:00 PM
On 05/21/11 09:31 AM, Edward Rutherford wrote:
> Shao Miller wrote:
>> On 5/20/2011 14:22, Edward Rutherford wrote:
>>>
>> And the sizes of those types can vary per implementation, too, if I
>> recall correctly.
>>
>>> So if I have the following:
>>>
>>> typedef struct __buffer_t {
>>>
>>> char *buffer;
>>> size_t size;
>>>
>>> } Buffer_t;
>>>
>>> I should have mimized padding since size_t is an unsigned long.
>>
>> It is? How did you determine that?
>>
>>> However, will size_t ever become an unsigned long long?
>>
>> It might, as far as I know.
>
> So standard-wise, how do I handle size_t in structures to minmize
> padding? Should size_t all ways follow after pointers in structures?
> And if I have integers in the structure, where should I put size_t?
The standard does not go into the specifics of padding.
It is a reasonably safe bet to assume sizeof(size_t) == sizeof(void*).
I would point size_t members after pointer members.
> <OT>
> According my style(9) man page, the suggestion is to:
>
> "When declaring variables in structures, declare them sorted by use,
> then
> by size (largest to smallest), then by alphabetical order. "
>
> This would lead to padding, right? But I would have to assume a size
> for size_t to follow that style.
> </OT>
It would, but in most uses, padding isn't really an issue.
--
Ian Collins