Topic: sizeof empty objects


Author: jimad@microsoft.com (Jim Adcock)
Date: 07 Dec 92 23:09:24 GMT
Raw View
In article <1992Dec3.195422.26393@cs.brown.edu> sdm@cs.brown.edu (Scott Meyers) writes:
|It's well known that the sizeof a class/struct may be greater than the sum
|of the sizeofs of its members, but if the sizeof of an empty object must be
|positive (which it must be) yet a compiler is allowed to allocate 0 bytes
|for an empty subobject, it becomes possible for the sizeof a class/struct
|to be LESS THAN the sum of the sizeofs of its members.
|
|Is this a good thing?

Whether its a good thing or not is a moot issue because its always been
the case that the sizeof a class/struct can be less than the sum of the
sizeofs of its members:

#include <iostream.h>

double A, B;
class X
{
public:
 X() : a(A), b(B){}
 double& a;
 double& b;
};

main()
{
 X x;
 cout << sizeof(X) << ' ' << (sizeof(x.a) + sizeof(x.b)) << '\n';

 return 0;
}




Author: sdm@cs.brown.edu (Scott Meyers)
Date: Thu, 3 Dec 1992 19:54:22 GMT
Raw View
I've been following this discussion of the size of empty objects with a
glazed look in my eyes, but when I saw Johan Bengtsson and Jim Adcock come
to a seeming agreement that it is possible for an object to have the same
address as one of its subobjects, I woke up with a start. Some time ago
Lars Nyman pointed out this:

  and ARM p164:
          "Objects of an empty class have a nonzero size".

It's well known that the sizeof a class/struct may be greater than the sum
of the sizeofs of its members, but if the sizeof of an empty object must be
positive (which it must be) yet a compiler is allowed to allocate 0 bytes
for an empty subobject, it becomes possible for the sizeof a class/struct
to be LESS THAN the sum of the sizeofs of its members.

Is this a good thing?

Scott

-------------------------------------------------------------------------------
What do you say to a convicted felon in Providence?  "Hello, Mr. Mayor."




Author: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
Date: Fri, 4 Dec 1992 12:51:14 GMT
Raw View
In article <1992Dec3.195422.26393@cs.brown.edu>, sdm@cs.brown.edu (Scott Meyers) writes:
> It's well known that the sizeof a class/struct may be greater than the sum
> of the sizeofs of its members, but if the sizeof of an empty object must be
> positive (which it must be) yet a compiler is allowed to allocate 0 bytes
> for an empty subobject, it becomes possible for the sizeof a class/struct
> to be LESS THAN the sum of the sizeofs of its members.
>
> Is this a good thing?

It's certainly not a bad thing. (Or rather I can see no reason why it should
be). Neither of the common uses on sizeof() (measuring an array or bit copying
blocks of memory) will be harmed.
--
    \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk
 .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
.__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
.  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
 # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W