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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:21:46+00:00 2026-05-17T18:21:46+00:00

This is the third question in the series of zero-size objects and subobjects today.

  • 0

This is the third question in the series of zero-size objects and subobjects today.
The standars clearly implies that member subobjects cannot have zero size whereas base class subobjects can.

struct X {}; //empty class, complete objects of class X have nonzero size
struct Y:X { char c; }; //Y's size may be 1 
struct Z {X x; char c;}; //Z's size MUST be greater than 1

Why not allow zero-size member-subobjects JUST LIKE zero-size base-class-subobjects?

TIA

EDITING after Konrad’s answer:
consider the following example:

struct X{}; 
struct D1:X{};
struct D2:D1, X {}; //D2 has 2 distinct subobjects of type X, can they both be 0 size and located at the same address?

If two base class subobjects of the same type X, as in my example, can be located at the same address, so should be able to member subobjects. If they cannot, then compiler treats this case specially and so can it treat Konrad’s example(see answer below) specially and disallow zero-size member subobjects if there are multiple of the same type in the same class. Where am I wrong?

  • 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-17T18:21:47+00:00Added an answer on May 17, 2026 at 6:21 pm

    The compiler could conditionally allow zero-size member objects as well as base classes, sure, but it would be more complex. The empty base class optimization always applies, regardless of type. Any time the compiler sees a class derive from a class with no data members, it can use the empty base class optimization.

    Following @Konrad Rudolphs example through, with member objects, it’d have to check the type, verify that no other object of the same type exists at that location, and then maybe apply your optimization. Well, unless the member object is located at the end of the containing class. If so, then the object’s “real” (non-zero) size would protrude past the end of the containing class, which would also be an error. That can never happen in the base class case because we know that the base class is located at the beginning of the derived class, and the derived class has non-zero size.

    So such an optimization would be more complex, more subtle, and more likely to break in unexpected ways.

    I can’t cite any cases off-hand where zero-size member objects would definitely break, but I’m not convinced that they don’t exist either. I’ve already pointed out a couple of limitations that don’t exist in the base-class case, and most likely, more exist. So the question is, how much complexity and uncertainty should the language allow just to make one rarely useful optimization possible?

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

Sidebar

Related Questions

I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
first of all this is my third question about web services here and i
this is my third question so far on stackoverflow :D i am defining files
This is a two-part question about adding a third-party library (JAR) to an Android
I'll preface this question by mentioning that while I'm far from a regular expressions
Update : It occurred to me after posting this question that the main downside
Without using any third party program to do this (i.e. without VMware ThinApp, U3
Anyone know how to do this without using a third party program? If there
I'm using a third-party library which accesses fonts through the GraphicsEnvironment: getAllFonts() call. This
This is kinda oddball, but I was poking around with the GNU assembler today

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.