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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:56:22+00:00 2026-06-10T12:56:22+00:00

I want to understand the layout of an object. So I executed with different

  • 0

I want to understand the layout of an object. So I executed with different orders of member variables. Everything came as expected, expect for following sequence.

#include <iostream>

using namespace std;
class Test1
{
public:
    int m_a;
    char m_b;
};


class Test
{
public:
    int m_b;
    Test1 m_t;
    char m_g;
    char m_c;
    char m_d;
    int m_e;
};

int main()
{
    Test t;
    cout<<(int*)(&t.m_b)<<endl;
    cout<<(int*)(&t.m_t.m_a)<<endl;
    cout<<(int*)(&t.m_t.m_b)<<endl;
    cout<<(int*)(&t.m_c)<<endl;
    cout<<(int*)(&t.m_d)<<endl;
    cout<<(int*)(&t.m_e)<<endl;
    cout<<sizeof(t)<<endl;
}

Output:

0xbfebbd6c
0xbfebbd70
0xbfebbd74
0xbfebbd79
0xbfebbd7a
0xbfebbd7c
20

Where as I expected 16.

But If i remove m_a from Test1, it is giving expected input(12).

#include <iostream>

using namespace std;
class Test1
{
public:
    char m_b;
};


class Test
{
public:
    int m_b;
    Test1 m_t;
    char m_g;
    char m_c;
    char m_d;
    int m_e;
};

int main()
{
    Test t;
    cout<<(int*)(&t.m_b)<<endl;
    cout<<(int*)(&t.m_t.m_b)<<endl;
    cout<<(int*)(&t.m_c)<<endl;
    cout<<(int*)(&t.m_d)<<endl;
    cout<<(int*)(&t.m_e)<<endl;
    cout<<sizeof(t)<<endl;
}

Output:

0xbf82e674
0xbf82e678
0xbf82e67a
0xbf82e67b
0xbf82e67c
12

Why there is the difference of 8 bytes if i remove integer which is exactly aligned to 4 bit boundary?

PS: I know this is implementation specific. I want to know how that implementation was done :). This was come into picture since i want to access private members, so trying to understand object layout !!!

  • 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-10T12:56:23+00:00Added an answer on June 10, 2026 at 12:56 pm

    With integer m_a sizeof(Test1) is 8 to align m_a to 4byte boundary. Without the int, it is only the size of th char.

    class Test
    {
    public:
        int m_b;     // 4
        Test1 m_t;   // 12
        char m_g;    // 13
        char m_c;    // 14
        char m_d;    // 15
    
        int m_e;     // 20
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to understand why the following is happening. My guess is that a
I have 2 different layout files that I want to use for modifying the
I'm reading different forums for hours but I want to understand what is getting
I want a layout almost exactly like the table layout but I understand I
I want to understand the role interface plays in inheritance between two classes. My
I want to understand the actual theory behind types rather than just learning about
I want to understand in which order are the elements of h_addr_list sorted when
I want to understand if code snippets are what I am looking for here.
I want to understand the use of xargs man in Rampion's code : screen
I want to understand the external linkage and internal linkage and their difference. I

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.