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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:10:57+00:00 2026-05-11T06:10:57+00:00

Consider these types: struct A {}; struct B : A { int i; };

  • 0

Consider these types:

  struct A {};   struct B : A { int i; }; 

sizeof(A) > 0 as required by the standard.

sizeof(B) should be 4 due to the empty base optimization. Yet on GCC 4.1.1 it’s 5 (I’m using a pack of 1 in this area). And inconsistently – some of my files are getting it, some are not. Can’t be sure what the differences are yet, we have a large prjoect.

On the other three compilers I’m using (by Microsoft and Freescale), I don’t have this problem. The empty base optimization is optional apparently, according to this article.

Is there a compiler option or pragma to tune this in GCC 4.1.1? I can work around the issue but I would like to understand what’s going on first. I Googled for a while and can’t seem to find anything.

  • 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. 2026-05-11T06:10:58+00:00Added an answer on May 11, 2026 at 6:10 am

    This always happens. I post immediately before I figure it out. Maybe the act of posting gets me thinking in a different way..

    So in my question the sample was a little bit over-simplified. It’s actually more like this:

    struct Base {}; struct C1 : Base { int i; } struct C2 : Base { C1 c; int i; } 

    sizeof(C1) is correctly 4 on all platforms, but sizeof(C2) is 9 instead of 8 on GCC. And… apparently GCC is the only thing that gets it right, according to the last bit of the article I linked to in the original question. I’ll quote it (from Nathan Meyers) here:

    A whole family of related ’empty subobject’ optimizations are possible, subject to the ABI specifications a compiler must observe. (Jason Merrill pointed some of these out to me, years back.) For example, consider three struct members of (empty) types A, B, and C, and a fourth non-empty. They may, conformingly, all occupy the same address, as long as they don’t have any bases in common with one another or with the containing class. A common gotcha in practice is to have the first (or only) member of a class derived from the same empty base as the class. The compiler has to insert padding so that they two subobjects have different addresses. This actually occurs in iterator adapters that have an interator member, both derived from std::iterator. An incautiously-implemented standard std::reverse_iterator might exhibit this problem.

    So, the inconsistency I was seeing was only in cases where I had the above pattern. Every other place I was deriving from an empty struct was ok.

    Easy enough to work around. Thanks all for the comments and answers.

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

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Quote from this FAQ: Is array of derived same as… May 11, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer Here - I'll start you off with a couple of… May 11, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer Check the script to see what is the actual wireshark… May 11, 2026 at 10:04 pm

Related Questions

In our code we used to have something like this: *(controller->bigstruct) = ( struct
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
Given that all the primitive data types and objects have memory allocated, it is
In my career I've come across two broad types of theory: physical theories and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.