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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:46:39+00:00 2026-06-09T04:46:39+00:00

I have a problem with bitfields in derived classes. With the g++ compiler, you

  • 0

I have a problem with bitfields in derived classes.

With the g++ compiler, you can assign __attribute__((packed)) to a class and it will pack bitfields. So

class A
{
  public:
    int one:10;
    int two:10;
    int three:10;
} __attribute__ ((__packed__));

takes up only 4 bytes. So far, so good.
However, if you inherit a class, like this

class B
{
  public:
    int one:10;
    int two:10;
} __attribute__ ((__packed__));

class C : public B
{
  public:
    int three:10;
} __attribute__ ((__packed__));

I would expect class C, which has the same content as class A above, to have the same layout as well, i.e. take up 4 bytes. However, C turns out to occupy 5 bytes.

So my question is, am I doing something wrong, and if so, what? Or is this a problem with the compiler? An oversight, a real bug?

I tried googling, but haven’t really come up with anything, apart from a difference between Linux and Windows (where the compiler tries to emulate MSVC), which I’m not interested in. This is just on Linux.

  • 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-09T04:46:40+00:00Added an answer on June 9, 2026 at 4:46 am

    I believe the problem is with B, which cannot easily be 2.5 bytes. It has to be at least 3 bytes.

    Theoretically, the derived class might be allowed to reuse padding from the base class, but I have never seen that happen.

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

Sidebar

Related Questions

I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
I have problem with consuming DbContext class in EF 4.3 CodeFirst approach. As for
I have problem with List<Image> List<Image> _Images = new List<Image>(); int currIndex = 0;
I have problem with css styles in aspx.cs file HttpContext.Current.Response.Write(<table class=\'bordered\' cellpadding=\'5\' color: \'red\'
I have problem in C#. I have a list box and a class called
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with pointers. This is working fine - int main(void){ char *w;
I have problem with deleting rows from table. I have a class for cell
I have this problem in C++: can I typedef a bitfield whose values come
I have problem in using ExceptionMatcher...My example spec: class DescribeBall extends \PHPSpec\Context { private

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.