Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: size_t in a struct |
| Date | 2011-05-21 18:00 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ir8uj5$gq1$1@speranza.aioe.org> (permalink) |
| References | <ir6bhi$fcu$1@speranza.aioe.org> <ir6d98$e50$1@dont-email.me> <ir6mjl$d08$1@speranza.aioe.org> <lntycpyp2p.fsf@nuthaus.mib.org> |
Keith Thompson wrote:
> Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> writes:
>> Shao Miller wrote:
>>> On 5/20/2011 14:22, Edward Rutherford wrote:
>>>> To avoid padding in structures, where is the best place to put size_t
>>>> variables?
>>>>
>>>> According the faq question 2.12
>>>> (http://c-faq.com/struct/padding.html), it says:
>>>>
>>>> "If you're worried about wasted space, you can minimize the effects
>>>> of padding by ordering the members of a structure based on their base
>>>> types, from largest to smallest."
>>>>
>>>>
>>> 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?
>
> Standard-wise, you don't. The standard requires at least enough padding
> to satisfy alignment requirements, but it says very little about what
> those requirements are.
>
> If you really absolutely need to avoid any possibility of padding,
> declare an array of unsigned char and use memcpy() to copy the
> individual members in an out.
>
>> <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 might, though I've never followed that kind of rule myself.
>
> Again, *why* are you so concerned about minimizing padding? What bad
> thing would happen if there were a little padding between members in
> your structs?
>
> If you'll tell us what your actual goal is, we'll be better able to help
> you.
Thanks for that information.
I want to avoid padding in order to minimize the size of my structs in
order to prevent caches misses where possible. I would expect that the
overhead from using memcpy and unsigned char arrays, would outweight the
savings from optimizing cache usage - happy to be corrected though.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
size_t in a struct Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-05-20 18:22 +0000
Re: size_t in a struct Shao Miller <sha0.miller@gmail.com> - 2011-05-20 14:52 -0400
Re: size_t in a struct Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-05-20 21:31 +0000
Re: size_t in a struct Ian Collins <ian-news@hotmail.com> - 2011-05-21 09:51 +1200
Re: size_t in a struct Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-05-24 21:34 +0300
Re: size_t in a struct Ian Collins <ian-news@hotmail.com> - 2011-05-25 07:54 +1200
Re: size_t in a struct "[the China Blue] Drill Baby Drill" <chine.bleu@yahoo.com> - 2011-05-20 15:06 -0700
Re: size_t in a struct Angel <angel+news@spamcop.net> - 2011-05-20 22:51 +0000
Re: size_t in a struct Keith Thompson <kst-u@mib.org> - 2011-05-20 15:39 -0700
Re: size_t in a struct Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-05-21 18:00 +0000
Re: size_t in a struct Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-21 14:12 -0600
Re: size_t in a struct Keith Thompson <kst-u@mib.org> - 2011-05-21 16:31 -0700
Re: size_t in a struct Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-22 10:08 +0100
Re: size_t in a struct Shao Miller <sha0.miller@gmail.com> - 2011-05-22 13:24 -0500
Re: size_t in a struct Ben Pfaff <blp@cs.stanford.edu> - 2011-05-22 10:37 -0700
Re: size_t in a struct Uncle Steve <stevet810@gmail.com> - 2011-05-22 14:18 -0400
Re: size_t in a struct Keith Thompson <kst-u@mib.org> - 2011-05-22 12:17 -0700
Re: size_t in a struct David Thompson <dave.thompson2@verizon.net> - 2011-06-01 16:33 -0400
Re: size_t in a struct Shao Miller <sha0.miller@gmail.com> - 2011-05-23 02:05 -0500
Re: size_t in a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-23 11:21 +0100
Re: size_t in a struct Shao Miller <sha0.miller@gmail.com> - 2011-05-23 19:40 -0500
Re: size_t in a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-24 01:25 +0100
Re: size_t in a struct Shao Miller <sha0.miller@gmail.com> - 2011-05-23 20:43 -0500
Re: size_t in a struct gordonb.d6jvy@burditt.org (Gordon Burditt) - 2011-05-22 17:48 -0500
Re: size_t in a struct Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-21 15:43 +0000
Re: size_t in a struct Keith Thompson <kst-u@mib.org> - 2011-05-20 11:59 -0700
Re: size_t in a struct Nobody <nobody@nowhere.com> - 2011-05-20 22:24 +0100
csiph-web