Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15777

Re: struct alignment

From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: struct alignment
Date 2012-01-12 06:56 +0000
Message-ID <87hb01v279.fsf@temporary-address.org.uk> (permalink)
References <jekkil$u7v$1@speranza.aioe.org> <lnipkh3lpg.fsf@nuthaus.mib.org>

Show all headers | View raw


Keith Thompson <kst-u@mib.org> writes:

> The compiler will (in fact, it *must*) insert whatever padding is
> necessary so that all the members can be accesssed correctly.  It may or
> may not insert more padding than it strictly needs to.
>
> The language standard guarantees that the first declared member is at
> offset 0, and that all other members are at increasing offsets in the
> order in which they're declared (the compiler can't rearrange
> members).

Does any compiler let you do this?  I have done things where the order
of a least some elements (usually the first or the last) matters, but
it's rare.

> There can be arbitrary padding between members, or after the last one.
>
> Some compilers have extensions that allow you to request tighter packing
> (less padding) than the default.  

I can think of cases where re-arranging could give significant space
savings without these risks.

In response to the obvious "why don't you do it?" there are a couple of
reasons.  One, of course, that it will vary from platform to platform.
The second is that quite often there is a logical order or partial order
of fields from a program logic point of view that is different to the
most efficient.

For example, you might want to start off with a small integer to say
what type of object you have, have a few pointers that point to various
type-specific things, then an integer reference count and then a pointer
to the next one in a list.

Shoving the two integers together could well save you some memory with
out the need for the compiler to generate fancy code (as in the "packed"
case) but at the loss of program source clarity.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

struct alignment "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-01-11 13:31 -0500
  Re: struct alignment Kleuske <kleuske@nowhere.net> - 2012-01-11 19:23 +0000
  Re: struct alignment "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-01-11 16:58 -0500
  Re: struct alignment Ralf Damaschke <rwspam@gmx.de> - 2012-01-11 22:08 +0000
  Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-11 14:42 -0800
    Re: struct alignment Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-12 06:56 +0000
      Re: struct alignment Ian Collins <ian-news@hotmail.com> - 2012-01-12 20:42 +1300
        Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-12 02:20 -0800
          Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-18 17:03 +0000
            Re: struct alignment Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 17:21 +0000
              Re: struct alignment jgk@panix.com (Joe keane) - 2012-01-19 21:42 +0000
                Re: struct alignment blp@cs.stanford.edu (Ben Pfaff) - 2012-01-19 13:57 -0800
            Re: struct alignment Stephen Sprunk <stephen@sprunk.org> - 2012-01-18 11:57 -0600
              Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-18 14:28 -0800
            Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-18 13:10 -0600
              Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-19 11:34 +0000
                Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-19 14:50 -0600
                Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-21 11:49 +0000

csiph-web