Topic: sizeof class with reference data members


Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/24
Raw View
What does the standard say of the sizeof of a class/struct with
reference data members?

   #include <iostream.h>

   struct Thing { int a,b,c,d; };

   int main() {
      Thing thing;

      const Thing& t=thing;
      cout << sizeof(t) << '\n'; // same as sizeof(Thing), on my system: 16

      struct X { const Thing& t; X(const Thing& t) : t(t) { } };
      X x(thing);
      cout << sizeof(x) << '\n'; // on my system: 4 or size of pointer
   }

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/05/24
Raw View
sbnaran@fermi.ceg.uiuc.edu (Siemel Naran) writes:
> What does the standard say of the sizeof of a class/struct with
> reference data members?

Nothing at all; why should it? It merely says that sizeof a class is
the number of bytes in an object of that class.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "John D. Hickin" <hickin@Hydro.CAM.ORG>
Date: 1999/05/25
Raw View
Siemel Naran wrote:
>
> What does the standard say of the sizeof of a class/struct with
> reference data members?
>

Section 5.3.3 Sizeof, paragraph 2, states that sizeof(r) where r is a
reference or a reference type, is the size of the referenced type
whereas sizeof(X), where X is a class, is the (implementation defined)
number of bytes in the representation of the class, which includes any
necessary padding.

It makes sense when you think about the result of sizeof(*p) for a
pointer expression p.

In your example, t is a Thing& and sizeof(t) is correctly reported as
sizeof(Thing). sizeof(X) is 4 which means that a reference requires 4
bytes in your implementation, assuming that there is no padding.

Regards, John.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/25
Raw View
Siemel Naran wrote:
>
> What does the standard say of the sizeof of a class/struct with
> reference data members?
>
>    #include <iostream.h>
>
>    struct Thing { int a,b,c,d; };
>
>    int main() {
>       Thing thing;
>
>       const Thing& t=thing;
>       cout << sizeof(t) << '\n'; // same as sizeof(Thing), on my system: 16
>
>       struct X { const Thing& t; X(const Thing& t) : t(t) { } };
>       X x(thing);
>       cout << sizeof(x) << '\n'; // on my system: 4 or size of pointer
>    }
>

The size must be an integer, and greater than 0. By implication, a class
object must be large enough to hold all of of it's non-static data
members, with some limits on the placement of padding bits and bytes,
but with no upper limit on the amount of padding. It doesn't say much
more than that. In practice, a reference is the semantic equivalent of a
'const pointer' with different syntax, so I'd expect the size of a
reference to be comparable to the size of a pointer.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Tom Payne <thp@hill.cs.ucr.edu>
Date: 1999/05/26
Raw View
In comp.std.c++ Siemel Naran <sbnaran@fermi.ceg.uiuc.edu> wrote:
: What does the standard say of the sizeof of a class/struct with
: reference data members?

For compatibility, class/struct objects whose members are of C data
types are called POD (plain old data) and must have the size and
layout that C prescribes.  Class/struct objects with reference
members, however, aren't POD and (AFIK) have no such layout or size
restrictions.

Tom Payne
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]