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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:52:04+00:00 2026-06-12T03:52:04+00:00

It is a simple question. Code first. struct A { int x; }; struct

  • 0

It is a simple question. Code first.

struct A {
    int x; 
};
struct B {
    bool y;
};
struct C {
    int x;
    bool y;
};

In main function, I call

cout << " bool : " << sizeof(bool) <<
     "\n int : " << sizeof(int) <<
     "\n class A : " << sizeof(A) <<
     "\n class B : " << sizeof(B) <<
     "\n class C : " << sizeof(C) << "\n";

And the result is

bool : 1
int : 4
class A : 4
class B : 1
class C : 8

Why is the size of class C 8 instead of 5?
Note that this is compiled with gcc in MINGW 4.7 / Windows 7 / 32 bit machine.

  • 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-12T03:52:05+00:00Added an answer on June 12, 2026 at 3:52 am

    The alignment of an aggregate is that of its strictest member (the member with the largest alignment requirement). In other words the size of the structure is a multiple of the alignment of its strictest (with the largest alignment requirement) member.

    struct D
    {
      bool a;
      // will be padded with char[7]
      double b; // the largest alignment requirement (8 bytes in my environment)
    };
    

    The size of the structure above will be 16 bytes because 16 is a multiple of 8. In your example the strictest type is int aligning to 4 bytes. That’s why the structure is padded to have 8 bytes. I’ll give you another example:

    struct E
    {
      int a;
      // padded with char[4]
      double b;
    };
    

    The size of the structure above is 16. 16 is multiple of 8 (alignment of double in my environment).

    I wrote a blog post about memory alignment for more detailed explanation
    http://evpo.wordpress.com/2014/01/25/memory-alignment-of-structures-and-classes-in-c-2/

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

Sidebar

Related Questions

Simple question, how make this code working ? public class T { public static
Hopefully a very simple question. But I'm using Code First with an MVC app
A simple question: Why does this the first code work but the seemingly identical
Consider the following simple code. struct test { test(int n):numelements(n){ arr = new int[numelements]
I am using Polymorphic Models . Simple Question: My code below works without using
ok simple question about including large mostly static html and asp.net code I am
Really simple question here but bugging me for long enough to ask. Code looks
i have this simple question please. I have this part of code which sets
Maybe a very simple question. How can I put in this code <Query> <Where>
I've got a simple question but it has got me confounded. The code below

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.