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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:52:28+00:00 2026-05-23T16:52:28+00:00

While answering one of the question, there was a discussion thread below my answer

  • 0

While answering one of the question, there was a discussion thread below my answer. Which suggests that depending on the access specifier (or may be the type of inheritance) private/protected/public the sizeof the class object may vary!

I still don’t understand from their brief discussion, how is that possible ?

  • 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-23T16:52:29+00:00Added an answer on May 23, 2026 at 4:52 pm

    Note new language for C++11 below

    In C++03, there is language that makes this possible, 9.2 [class.mem]/12 (emphasis mine):

    Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

    So given this definition:

    class Foo
    {
        char a; //8 bits
        // a must come before b, so 3 bytes of padding have to go here to satisfy alignment
        int b; //32 bits
        char c; //8 bits
        // 24 bits of padding required to make Foo a multiple of sizeof(int)
    };
    

    on a system with 32 bit (int) alignment, the compiler is not allowed to reorder c to come before b, forcing the insertion of additional padding padding in between a and b, and after c to the end of the object (making sizeof(Foo) == 12). However, for this:

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

    a and (b and c) are separated by an access specifier, so the compiler is free to perform such reordering, making

    memory-layout Foo
    {
        char a; // 8 bits
        char c; // 8 bits
        // 16 bits of padding
        int b; // 32 bits
    };
    

    sizeof(Foo) == 8.

    In C++11, the language changes slightly. N3485 9.2 [class.mem]/13 says (emphasis mine):

    Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause 11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

    This means that in C++11, in the above example (separated by 3 publics), the compiler is still not allowed to perform the reordering. It would have to be something like

    class Foo
    {
        char a;
    public:
        int b;
    protected:
        char c;
    };
    

    , which places a, b, and c with different access control.

    Note that under the C++11 rules, given a definition like:

    class Foo
    {
        char a;
    public:
        int b;
    protected:
        char c;
    public:
        int d;
    };
    

    the compiler must put d after b, even though they are separated by access specifiers.


    (That said, I’m not aware of any implementation that actually takes advantage of the latitude offered by either standard)

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

Sidebar

Related Questions

While answering a question on SO yesterday, I noticed that if an object is
While answering this question I noticed that I got the following dialog while atempting
While answering this question DOM parser: remove certain attributes only I noticed that some
While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
While answering another question, I thought of the following example: void *p; unsigned x
While answering this question regarding safe escaping of filename with spaces (and potentially other
Before answering this question, understand that I am not asking how to create my
While answering this question I came across an interesting difference in the overload resolution
While answering a particular question here in SO I stumbled upon a peculiar issue
In answering another question* on SO, and the subsequent comment discussion, I ran into

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.