Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:45:06+00:00 2026-06-17T07:45:06+00:00

Following GCC __attribute__(packed ) will pack to byte boundary, aligned is used for which

  • 0

Following GCC __attribute__(packed ) will pack to byte boundary, aligned is used for which purpose :–

u8 rx_buf[14] __attribute__((aligned(8)));  
struct S { short f[3]; } __attribute__ ((aligned (8)));

above array will be of 16 byte, am I right.

means sizeof(rx_buff) will be 16 byte .. i.e 2 byte alignment at end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:45:07+00:00Added an answer on June 17, 2026 at 7:45 am

    The answer to your question is no. The aligned attribute does not change the sizes of variables it is applied to, but the situation is slightly different for structure members. To quote the manual,

    aligned (alignment)

    This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

    int x __attribute__ ((aligned (16))) = 0;
    

    causes the compiler to allocate the global variable x on a 16-byte boundary.

    and,

    packed

    The packed attribute specifies that a variable or structure field should have the smallest possible alignment — one byte for a variable,
    and one bit for a field, unless you specify a larger value with the
    aligned attribute.

    Here is a structure in which the field x is packed, so that it immediately follows a:

    struct foo
    {
        char a;
        int x[2] __attribute__ ((packed));
    };
    

    Note that the aligned attribute may change the memory layout of structures, by inserting padding between members. Subsequently, the size of the structure will change. For instance:

    struct t {
        uint8_t  a __attribute__((aligned(16))); /* one byte plus 15 as padding here */
        uint16_t b __attribute__((aligned(16)));
    };
    

    would lead to 15 bytes of padding after a whereas the default alignment for the target architecture might have resulted in less. If you specified the packed attribute for the structure and lost the aligned attributes the structure would have a size of 3 bytes.
    Here’s an illustration of how the memory layout of a structure might look like in each case.

    struct t with no attributes and default alignment on 8-byte boundary:

    +-+-------+--+-------+
    |a|       |bb|       |
    +-+-------+--+-------+
    

    struct t when a and b are aligned on 16-byte boundaries:

    +-+---------------+--+---------------+
    |a|    padding    |bb|    padding    |
    +-+---------------+--+---------------+
    

    struct t when a and b have no alignment restrictions and t is packed:

    +-+--+
    |a|bb|
    +-+--+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc does not like the following code: inline const plus(unsigned x,unsigned y) __attribute__((pure)); inline
I have Googled information on gcc's __attribute__ ((aligned)) to learn more about how to
I have the following code: #include <stdio.h> int main(void) { int x __attribute__ ((aligned
The following GCC inline asm is taken from LuaJit's coco library. Can someone provide
Running the following program on gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 and Intel(R) Core(TM)2 Duo CPU,
I compiled the following program with gcc -fprofile-arcs -ftest-coverage test.c: int main() { int
I've recently found myself using the following macro with gcc 4.5 in C++11 mode:
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
gcc 4.4.2 I have the following code: char channels[] = NumberOfChannel = [2]; sscanf(channels,
The following fails to compile (with gcc 4.2.1 on Linux, anyway): template< typename T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.