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

  • Home
  • SEARCH
  • 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 3441672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:34:54+00:00 2026-05-18T08:34:54+00:00

Consider: struct mystruct_A { char a; int b; char c; } x; struct mystruct_B

  • 0

Consider:

struct mystruct_A
{
   char a;
   int b;
   char c;
} x;

struct mystruct_B
{
   int b;
   char a;
} y;

The sizes of the structures are 12 and 8 respectively.

Are these structures padded or packed?

When does padding or packing take place?

  • 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-05-18T08:34:54+00:00Added an answer on May 18, 2026 at 8:34 am

    Padding aligns structure members to “natural” address boundaries – say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following “gaps” into your first structure:

    struct mystruct_A {
        char a;
        char gap_0[3]; /* inserted by compiler: for alignment of b */
        int b;
        char c;
        char gap_1[3]; /* -"-: for alignment of the whole struct in an array */
    } x;
    

    Packing, on the other hand prevents compiler from doing padding – this has to be explicitly requested – under GCC it’s __attribute__((__packed__)), so the following:

    struct __attribute__((__packed__)) mystruct_A {
        char a;
        int b;
        char c;
    };
    

    would produce structure of size 6 on a 32-bit architecture.

    A note though – unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider these two structures: struct Task { public Int32 Id; public String Name; public
Consider this POD: struct T { int i; char c; }; In which C++
Consider the following code struct foo { const int txt_len; const int num_len; char
Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider: struct { char *rcssMonitoredServices; int nSomeVar; } appinfo; In conventional use: appinfo.rcssMonitoredServices=strdup(something); However
Consider these types: struct A {}; struct B : A { int i; };
Consider something like: struct Parameter { int a; Parameter(){a = 0;} void setA(int newA){a
Consider the following: typedef struct { int a; int b; int c; int d;
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following struct: public struct vip { string email; string name; int category;

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.