aligned / packed

Discussion in 'C++' started by Travis, Apr 28, 2008.

  1. Travis

    Travis Guest

    Hi all -

    Most of my experience in c++ has been upper level. I am just now
    getting into lower level stuff and I see this alot

    class foo {
    ....
    } __attribute__((aligned(1), packed));

    I am having trouble finding any documentation online that explains
    aligned and packed to an outsider or low level work so I thought I
    would put it to this awesome community. :)
    Travis, Apr 28, 2008
    #1
    1. Advertising

  2. On 2008-04-28 20:29, Travis wrote:
    > Hi all -
    >
    > Most of my experience in c++ has been upper level. I am just now
    > getting into lower level stuff and I see this alot
    >
    > class foo {
    > ...
    > } __attribute__((aligned(1), packed));
    >
    > I am having trouble finding any documentation online that explains
    > aligned and packed to an outsider or low level work so I thought I
    > would put it to this awesome community. :)


    It is a gcc extension that allows you to control the layout (in memory)
    of the class members, search the documentation at gcc.gnu.org for more
    information.

    --
    Erik Wikström
    Erik Wikström, Apr 28, 2008
    #2
    1. Advertising

  3. Travis

    red floyd Guest

    Travis wrote:
    > Hi all -
    >
    > Most of my experience in c++ has been upper level. I am just now
    > getting into lower level stuff and I see this alot
    >
    > class foo {
    > ...
    > } __attribute__((aligned(1), packed));
    >
    > I am having trouble finding any documentation online that explains
    > aligned and packed to an outsider or low level work so I thought I
    > would put it to this awesome community. :)


    In addition to what Erik told you, you can also try asking in gnu.g++.help
    red floyd, Apr 28, 2008
    #3
  4. Travis

    Jim Langston Guest

    Erik Wikström wrote:
    > On 2008-04-28 20:29, Travis wrote:
    >> Hi all -
    >>
    >> Most of my experience in c++ has been upper level. I am just now
    >> getting into lower level stuff and I see this alot
    >>
    >> class foo {
    >> ...
    >> } __attribute__((aligned(1), packed));
    >>
    >> I am having trouble finding any documentation online that explains
    >> aligned and packed to an outsider or low level work so I thought I
    >> would put it to this awesome community. :)

    >
    > It is a gcc extension that allows you to control the layout (in
    > memory) of the class members, search the documentation at gcc.gnu.org
    > for more information.


    In the Microsoft world it is something like:
    #pragma packed

    What it does is removes padding bytes inside of the data structures. For
    instance, 4 byte integers want to start on a byte that is evenly divisible
    by 4 (or 2 on some systems). So if you had the structure:

    struct Foo
    {
    char x;
    int y;
    };

    The size of this would not be 5 as may be expected, but 8. The reason is
    there are 3 padding bytes added between x and y so that y starts on a 4 byte
    boundary. Normally, this is not a problem, but sometimes it can be an
    issue. For example, if someone were attempting to read a binary file into
    this structure that had no padding bytes. It would not align up correctly.
    Which is why compilers usually supply some way to remove padding bytes
    inside of structures.


    --
    Jim Langston
    Jim Langston, Apr 28, 2008
    #4
  5. Travis

    Travis Guest

    On Apr 28, 12:13 pm, "Jim Langston" <> wrote:
    > Erik Wikström wrote:
    > > On 2008-04-28 20:29, Travis wrote:
    > >> Hi all -

    >
    > >> Most of my experience in c++ has been upper level. I am just now
    > >> getting into lower level stuff and I see this alot

    >
    > >> class foo {
    > >> ...
    > >> } __attribute__((aligned(1), packed));

    >
    > >> I am having trouble finding any documentation online that explains
    > >> aligned and packed to an outsider or low level work so I thought I
    > >> would put it to this awesome community. :)

    >
    > > It is a gcc extension that allows you to control the layout (in
    > > memory) of the class members, search the documentation at gcc.gnu.org
    > > for more information.

    >
    > In the Microsoft world it is something like:
    > #pragma packed
    >
    > What it does is removes padding bytes inside of the data structures.  For
    > instance, 4 byte integers want to start on a byte that is evenly divisible
    > by 4 (or 2 on some systems).  So if you had the structure:
    >
    > struct Foo
    > {
    >    char x;
    >    int y;
    >
    > };
    >
    > The size of this would not be 5 as may be expected, but 8.  The reason is
    > there are 3 padding bytes added between x and y so that y starts on a 4 byte
    > boundary.  Normally, this is not a problem, but sometimes it can be an
    > issue.  For example, if someone were attempting to read a binary file into
    > this structure that had no padding bytes.  It would not align up correctly.
    > Which is why compilers usually supply some way to remove padding bytes
    > inside of structures.
    >
    > --
    > Jim Langston
    >


    awesome thanks for the info. so used in low level stuff for the sake
    of compactness and speed?

    disclosure - it's an embedded device.
    Travis, May 1, 2008
    #5
  6. Travis

    Ian Collins Guest

    Travis wrote:
    >
    > awesome thanks for the info. so used in low level stuff for the sake
    > of compactness and speed?
    >
    > disclosure - it's an embedded device.


    That depends on the platform, it may save space at the expense of
    decreased performance due to misaligned accesses requiring multiple bus
    cycles.

    --
    Ian Collins.
    Ian Collins, May 1, 2008
    #6
  7. On Apr 28, 2:29 pm, Travis <> wrote:
    > Hi all -
    >
    > Most of my experience in c++ has been upper level. I am just now
    > getting into lower level stuff and I see this alot
    >
    > class foo {
    > ...
    >
    > } __attribute__((aligned(1), packed));
    >
    > I am having trouble finding any documentation online that explains
    > aligned and packed to an outsider or low level work so I thought I
    > would put it to this awesome community. :)


    Refer to the following link for some background on byte alignment and
    padding:

    http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    --
    EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
    Sequence diagram based systems engineering tool
    EventHelix.com, May 1, 2008
    #7
  8. Travis

    James Kanze Guest

    On May 1, 1:33 am, Ian Collins <> wrote:
    > Travis wrote:


    > > awesome thanks for the info. so used in low level stuff for
    > > the sake of compactness and speed?


    > > disclosure - it's an embedded device.


    > That depends on the platform, it may save space at the expense
    > of decreased performance due to misaligned accesses requiring
    > multiple bus cycles.


    It may even increase the space. On many machines, accessing
    misaligned data requires more instructions, so you increase the
    code space.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 2, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. umberto
    Replies:
    4
    Views:
    556
    umberto
    Oct 24, 2003
  2. Chris
    Replies:
    6
    Views:
    494
    Chris Uppal
    Nov 7, 2004
  3. Sameer
    Replies:
    12
    Views:
    869
    Roedy Green
    Oct 6, 2005
  4. Delali Dzirasa

    int packed as hex with memcpy

    Delali Dzirasa, Oct 8, 2003, in forum: C++
    Replies:
    16
    Views:
    864
    Delali Dzirasa
    Oct 10, 2003
  5. Philippe Martin

    Passing a packed C structure to a c module

    Philippe Martin, Apr 14, 2006, in forum: Python
    Replies:
    8
    Views:
    365
    Philippe Martin
    Apr 14, 2006
Loading...

Share This Page