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 8808041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:31:11+00:00 2026-06-14T02:31:11+00:00

Take this example class A { public: int a; char b; int c; };

  • 0

Take this example

class A
{
public:
  int a; 
  char b;
  int c;
};

Every compiler (for x86, 32 or 64 bit) I see allocates 12 bytes for class A, instead of 9. So they are aligning b to the integer boundary or bus boundary you can say. My question is if this is in C++ standard to do so and if there are any compilers who does not do like that.

  • 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-14T02:31:13+00:00Added an answer on June 14, 2026 at 2:31 am

    The C++ standard specifies that:

    • objects have an alignment requirement of which their size is a multiple (so if int is 4 bytes wide, then it requires an alignment of 1, 2 or 4 bytes, depending on the implementation).
    • member objects (if they are not separated by access specifiers such as public) are all allocated in the order they’re declared
    • and they are allocated respecting their alignment requirements.

    So no, the standard doesn’t say exactly that the class should have a size of 12 bytes.

    But it does say that b should be allocated after a, and that c should be allocated after b.

    On a platform where int is 4 bytes wide, and requires 4-byte alignment, this leaves 12 bytes as the smallest valid size:

    • a takes the first 4 bytes
    • b takes one byte
    • c needs 4 bytes, but must be allocated on a 4-byte boundary. b ended one byte past such a boundary, so the next valid position to place c at is found by inserting 3 bytes of padding.

    So the total size of the class ends up being the size of the members (4 + 1 + 4 = 9) plus three bytes of padding, for a total of 12.

    There is another rule which has no effect here, but which would matter if you had defined the members in the order a, c, b instead.

    The containing class (A) inherits the alignment requirement from the strictest-aligned member object. That is, because it contains an int, it has the same alignment requirement as an int does. And because the object’s total size must be a multiple of its alignment requirement, a class containing the members in the order a, b, c would still require 12 bytes of storage. It’d just shift the 3 bytes of padding to the end of the class, instead of between b and c.

    However, in some other cases, reordering members in descending order of size can sometimes reduce the size of a class.

    Suppose we’d had a class like this instead:

    class B {
      char a;
      double b;
      int c;
    };
    

    This would have required 24 bytes of storage (1 bytes for a, 8 byte for b, and 4 bytes for c, but then to ensure b ends up on an 8-byte boundary, we’d need 7 bytes of padding between a and b, and to ensure that the whole class ends up with a size that is a multiple of 8, we need another 4 bytes after c.

    But reordering the members according to size, like this:

    class B {
      double b;
      int c;
      char a;
    };
    

    results in a class requiring only 16 bytes:

    the same 1 + 4 + 8 bytes for the member objects themselves, but now c is already aligned on a 4-byte boundary (because it comes after b which ends on an 8-byte boundary), and a never needs any alignment, so the only alignment we need is to ensure that B has a size that is a multiple of 8. The members take 13 bytes, so we can add 3 bytes of padding, and the class ends up at 16 bytes, 33% smaller than the first version.

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

Sidebar

Related Questions

Take this class as example: public class Category : PersistentObject<int> { public virtual string
Take this class for example: public class Applicant : UniClass<Applicant> { [Key] public int
Take this example: public class foo { public int[] func() { int arr[] =
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Take this program as an example: class Piece { public: Piece(bool color); protected: bool
Take the following class as an example: class Sometype { int someValue; public Sometype(int
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I just wanted to confirm the difference here, take this as an example: class
Please take a look on the following example: class Base { protected: int m_nValue;
Take these two classes for example (C++) class B1 { public: }; class B2

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.