Does gcc has compile option for bytes alignment?

Discussion in 'C++' started by Allen, Jan 7, 2009.

  1. Allen

    Allen Guest

    I know how to specify bytes alignment for a struct, i.e. using
    __attribute__((aligned(4))).
    But it is unconvenient because it must modify all struct, union and
    class declaration.
    Is there a compile option for gcc to define global bytes alignment?

    For example, something looks like
    #gcc -faligned4 -o test test.cpp

    Thank you.
    Allen
     
    Allen, Jan 7, 2009
    #1
    1. Advertising

  2. Allen

    Lionel B Guest

    On Wed, 07 Jan 2009 01:42:21 -0800, Allen wrote:

    > I know how to specify bytes alignment for a struct, i.e. using
    > __attribute__((aligned(4))).
    > But it is unconvenient because it must modify all struct, union and
    > class declaration.
    > Is there a compile option for gcc to define global bytes alignment?
    >
    > For example, something looks like
    > #gcc -faligned4 -o test test.cpp


    This ng does not deal with compiler-specific issues; you're more likely to
    get help in a gcc forum such as gnu.gcc.help or gnu.g++.help

    Thanks,

    --
    Lionel B
     
    Lionel B, Jan 7, 2009
    #2
    1. Advertising

  3. On Jan 7, 9:42 am, Allen <> wrote:
    > I know how to specify bytes alignment for a struct, i.e. using
    > __attribute__((aligned(4))).
    > But it is unconvenient because it must modify all struct, union and
    > class declaration.


    Why would you want to do that?

    > Is there a compile option for gcc to define global bytes alignment?


    There probably is:
    http://gcc.gnu.org/onlinedocs/gcc-4...ng-Pragmas.html#Structure_002dPacking-Pragmas

    --
    Max
     
    Maxim Yegorushkin, Jan 7, 2009
    #3
  4. Allen

    Allen Guest

    On 1ÔÂ7ÈÕ, ÏÂÎç6ʱ26·Ö, Maxim Yegorushkin <>
    wrote:
    > On Jan 7, 9:42 am, Allen <> wrote:
    >
    > > I know how to specify bytes alignment for a struct, i.e. using
    > > __attribute__((aligned(4))).
    > > But it is unconvenient because it must modify all struct, union and
    > > class declaration.

    >
    > Why would you want to do that?
    >
    > > Is there a compile option for gcc to define global bytes alignment?

    >
    > There probably is:http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Structure_002dPacking-Pra...
    >
    > --
    > Max


    I have tested option -fpack-struct=8 and #pragma pack(8), both fails
    to take effects.
    Only __attribute__((aligned(8)) works.

    Allen
     
    Allen, Jan 7, 2009
    #4
  5. On Jan 7, 1:54 pm, Allen <> wrote:
    > On 1ÔÂ7ÈÕ, ÏÂÎç6ʱ26·Ö, Maxim Yegorushkin <>
    > wrote:
    >
    > > On Jan 7, 9:42 am, Allen <> wrote:

    >
    > > > I know how to specify bytes alignment for a struct, i.e. using
    > > > __attribute__((aligned(4))).
    > > > But it is unconvenient because it must modify all struct, union and
    > > > class declaration.

    >
    > > Why would you want to do that?

    >
    > > > Is there a compile option for gcc to define global bytes alignment?

    >
    > > There probably is:http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Structure_002dPacking-Pra...

    >
    > I have tested option -fpack-struct=8 and #pragma pack(8), both fails
    > to take effects.


    How did you check that?

    --
    Max
     
    Maxim Yegorushkin, Jan 7, 2009
    #5
  6. Allen

    Allen Guest

    On Jan 7, 10:31 pm, Maxim Yegorushkin <>
    wrote:
    > On Jan 7, 1:54 pm, Allen <> wrote:
    >
    >
    >
    > > On 1ÔÂ7ÈÕ, ÏÂÎç6ʱ26·Ö, Maxim Yegorushkin <>
    > > wrote:

    >
    > > > On Jan 7, 9:42 am, Allen <> wrote:

    >
    > > > > I know how to specify bytes alignment for a struct, i.e. using
    > > > > __attribute__((aligned(4))).
    > > > > But it is unconvenient because it must modify all struct, union and
    > > > > class declaration.

    >
    > > > Why would you want to do that?

    >
    > > > > Is there a compile option for gcc to define global bytes alignment?

    >
    > > > There probably is:http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Structure_002dPacking-Pra...

    >
    > > I have tested option -fpack-struct=8 and #pragma pack(8), both fails
    > > to take effects.

    >
    > How did you check that?
    >
    > --
    > Max


    union Test
    {
    char c;
    int i;
    float f;
    };

    I print sizeof(Test), when using -fpack-struct=8 and #pragma pack(8)
    it outputs 4, while using __attribute__((aligned(8))) it outputs 8,
    i.e. as same as set value.

    Allen
     
    Allen, Jan 8, 2009
    #6
  7. On Jan 8, 2:19 am, Allen <> wrote:
    > On Jan 7, 10:31 pm, Maxim Yegorushkin <>
    > wrote:
    >
    >
    >
    > > On Jan 7, 1:54 pm, Allen <> wrote:

    >
    > > > On 1ÔÂ7ÈÕ, ÏÂÎç6ʱ26·Ö, Maxim Yegorushkin <>
    > > > wrote:

    >
    > > > > On Jan 7, 9:42 am, Allen <> wrote:

    >
    > > > > > I know how to specify bytes alignment for a struct, i.e. using
    > > > > > __attribute__((aligned(4))).
    > > > > > But it is unconvenient because it must modify all struct, union and
    > > > > > class declaration.

    >
    > > > > Why would you want to do that?

    >
    > > > > > Is there a compile option for gcc to define global bytes alignment?

    >
    > > > > There probably is:http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Structure_002dPacking-Pra...

    >
    > > > I have tested option -fpack-struct=8 and #pragma pack(8), both fails
    > > > to take effects.

    >
    > > How did you check that?

    >
    > > --
    > > Max

    >
    > union Test
    > {
    > char c;
    > int i;
    > float f;
    >
    > };
    >
    > I print sizeof(Test), when using -fpack-struct=8 and #pragma pack(8)
    > it outputs 4, while using __attribute__((aligned(8))) it outputs 8,
    > i.e. as same as set value.


    The description says "... a set of #pragma directives which change the
    maximum alignment of members of structures..." It is probably not what
    you want.

    --
    Max
     
    Maxim Yegorushkin, Jan 8, 2009
    #7
    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. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    448
    Mark McIntyre
    Nov 2, 2004
  2. Replies:
    5
    Views:
    388
    Nathan Addy
    Sep 17, 2005
  3. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
  4. Replies:
    3
    Views:
    396
    James Kanze
    Jul 20, 2007
  5. fAnSKyer
    Replies:
    2
    Views:
    566
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page