0

Background: I don't know a lot about memory location neither how zero size objects work, nor how to manipulate them.

Since a base class subobject of a standard layout class type with no non-static data member have zero size ( source 1 ), I expect that in the following code struct B has a zero size base-class subobject

struct A{};
struct B:A{};
int main(){A a; B b;}

Both a and bobjects have a size of 1 byte, but in the draft its said ( source_2 ):

The address of a non-bit-field subobject of zero size is the address of an unspecified byte of storage occupied by the complete object of that subobject.

So the address of the base-class subobject is the addres of an unspecified byte occupied by b, but it only has 1 byte, so b and its base-class subobject share the same address? If I didn't miss anything and the conclusion is right, how zero size subobjects are handled when "pointered" to?

  • `how zero size subobjects are handled when "pointered" to?` Isn't this exactly what your quote states? `The address of a non-bit-field subobject of zero size is the address of an unspecified byte of storage occupied by the complete object of that subobject.`. – tkausl Nov 17 '21 at 02:48
  • _I don't know a lot about memory location neither how zero size objects work, nor how to manipulate them_ -- Have you considered perhaps _learning this first_? If you don't really have much knowledge of layout, then the answer may not be as clear to follow. In practice, the uniqueness of an address doesn't and shouldn't ordinarily matter. The quote just discusses effective layout of objects in memory. If it's zero, then there's no offset when dereference. If it's nonzero, then there's a nonzero offset. – Human-Compiler Nov 17 '21 at 02:54
  • @Human-Compiler. You are right. I thought of "learning this", somewhat through this question. I'm not finding a good way to dive into this content properly –  Nov 17 '21 at 02:57
  • Have you tried printing the address of your two objects? I am uncertain if you are asking if `a` and `b` share the same address or not. – Dúthomhas Nov 17 '21 at 02:58
  • Two distinct objects cannot have the same address. The empty base optimisation means, in your case, that the `A`-part of a `B` consumes no memory - and a side effect can be (but is not guaranteed to be) that the address of the `A`-part can be same address as the object as a whole. In your code `a` and `b` are distinct objects (`a` is not part of `b`) so they cannot have the same address. Also, the standard requires that `sizeof` gives a non-zero result for all objects and instantiable types. `a` will not have size zero, even if the empty base optimisation is applied to the `A` part of a `B`. – Peter Nov 17 '21 at 08:41

1 Answers1

0

So the address of the base-class subobject is the addres of an unspecified byte occupied by b, but it only has 1 byte, so b and its base-class subobject share the same address?

Yes.

If I didn't miss anything and the conclusion is right, how zero size subobjects are handled when "pointered" to?

The value of the pointer is the address of the (sub-)object. Same as (sub-)objects of non-zero size.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Maybe my following question reveals immaturity of my understanding in object model. But it points to a zero size? I mean, it points to something that doesn't exist? the complete object have the byte storage even there isn't anything useful in there, but a pointer to a zero size subobject? I can't figure out it –  Nov 17 '21 at 02:53
  • That’s just language lawyering to avoid saying that it points to anything in particular — only that the address of both things are the same. – Dúthomhas Nov 17 '21 at 02:55
  • @Roman A pointer to the subobjects points to "an unspecified byte of storage occupied by the complete object", as per the rule. Nothing more, nothing less. Indeed, an object that doesn't use storage won't have anything useful in its storage. – eerorika Nov 17 '21 at 03:01
  • @eerorika. Why even have such address, for the zero size subobjects? –  Nov 17 '21 at 03:05
  • @Roman So that you can point / refer to the subobject. – eerorika Nov 17 '21 at 03:11
  • @eerorika, What you gain by being able to point / refer to a subojbject that have nothing? Maybe too much question here, but i'm still struggling in this content. –  Nov 17 '21 at 14:00
  • 1
    @Roman Similar thing that you gain by being able to point / refer to objects that have something. You can call member functions, and you can compare whether the object is same as another object etc. – eerorika Nov 17 '21 at 14:14