Topic: What's in an object ?


Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/14
Raw View
AllanW {formerly AllanW@my-dejanews.com} wrote:
....
> > For non-POD classes, it is implementation dependent.
>
> Does it say so in the standard? Where? And what would be the point
> of doing so?

The layout of structs is always implmentation dependent, but there are a
lot of rules that apply only to POD structs, for the specific purpose of
allowing compatibility with C code. Those rules are (mostly) in section
9. Just search for "POD".
---
[ 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: "AllanW {formerly AllanW@my-dejanews.com}" <allan_w@my-dejanews.com>
Date: 1999/04/14
Raw View
In article <7esb1j$o1q$1@mulga.cs.mu.OZ.AU>,
  fjh@cs.mu.OZ.AU (Fergus Henderson) wrote:
> Brendan Cullen <bcullen@xilinx.com> writes:
>
> >I have a class. It has data members and only non-static and non-virtual
> >functions.  I create 2 objects of that class. What's in each actual
> >object ?
>
> If, in addition to the constraints you already specified, your class
> has no user-declared constructors, no private or protected non-static
> data members, and no base classes, then it is an "aggregate" [1]; and if,
> in addition to being an aggregate, it has no non-static data members of
> type pointer to member, non-POD-struct, non-POD-union (or array of such
> types) or reference, and has no user-defined copy assignment operator
> and no user-defined destructor, then it is a "POD-struct" [2].
> Here "POD" stands for "plain old data".
>
> If it is not a POD-struct, then the contents of each object are
> pretty much entirely implementation-dependent.
>
> If it is a POD-struct, then there are rules about layout compatibility [3]
> which constrain the representation quite a bit.

This is all true, but I don't think it answers what Brendan was asking.

> > In particular I'm thinking of member functions.  Data members
> >are unique to each object.  But what about member function definitions
> >?

Conceptually, both objects contain all of the class members,
including the member functions, even for classes with no virtual
function members. This helps to explain the syntax:
    object.function(arguments)
because function is PART of the object. It also helps to teach
what happens with polymorphism, i.e. virtual functions. Once you
start using virtual functions, vehicle1.start() might end up
calling a completely different function than vehicle2.start().
Even though this doesn't apply to simple classes, it is still
the same conceptually.

In reality, compilers know that you don't have unlimited RAM. Since
any one class can only have one member function for a given function
signature, the compiler calls that -- so if vehicle1 and vehicle2
are both instances of Car, then they both call the same function
named Car::start(). Again, this is true whether or not you have any
virtual functions.

An exception might be made for inline functions. Here you're telling
the compiler that execution speed counts more than size and compile
speed, and so you're asking it to create one copy in memory per
call, rather than one per class. But this is still different than
having one copy in memory per object -- since C++ programs cannot
modify member functions, there's never any reason to instanciate a
member function along with an object.

> For non-POD classes, it is implementation dependent.

Does it say so in the standard? Where? And what would be the point
of doing so?

> Most implementations normally use a single `vtable' pointer (though
> sometimes more, in particur in the case of multiple inheritence)
> in each object which points to a table of virtual functions.
> Non-virtual functions are usually not stored in the object at all.
> However, the standard does not guarantee this.

Vtable pointers are not mentioned in the standard; they are an
implementation detail. Even so, surely the use of a vtable pointer
does not constitute instanciation of the member function itself?

Besides, vtable pointers are not used unless the class (or some base
class) has at least one virtual function. Brendan wasn't talking
about that.

> I have heard that some C++ implementations use representations which
> are quite different from the usual ones, in order to facilitate
> better binary compatibility and cheaper recompilation.  With these
> implementations, all non-POD objects have the same size (the compiler
> implicitly inserts an extra level of indirection) so that if only
> the private part of a class Foo has been modified then you don't need
> to recompile the classes that use Foo.

If this is true, it had better be transparent to the user. For instance,
it better not always be true that sizeof(foo)==sizeof(&foo). And if it
is transparent to the user, then this is irrelevant to what Brendan was
asking.

----
Allan_W@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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: Patrick =?iso-8859-1?Q?M=E9rissert=2DCoffini=E8res?= <pamc@club-internet.fr>
Date: 1999/04/12
Raw View
Hello,

There is a very interesting book by Stan Lippman, called "Inside The C++
Object Model" which seems to be what you are looking for, if I have
understood  your question. It's a Addison Wesley publication. I think it
should be fairly easy to find.

Brendan Cullen wrote:

> Hi folks,
>
> I have a class. It has data members and only non-static and non-virtual
> functions.  I create 2 objects of that class. What's in each actual
> object ? In particular I'm thinking of member functions.  Data members
> are unique to each object.  But what about member function definitions
> ?  Why have more than 1definition of each function ?  This may possibly
> not be defined for the language.  It may be implementation specific.
> I'm curious though !
>
> Regards,
>
> Brendan
---
[ 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: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1999/04/12
Raw View
Brendan Cullen <bcullen@xilinx.com> writes:

>I have a class. It has data members and only non-static and non-virtual
>functions.  I create 2 objects of that class. What's in each actual
>object ?

If, in addition to the constraints you already specified, your class
has no user-declared constructors, no private or protected non-static
data members, and no base classes, then it is an "aggregate" [1]; and if,
in addition to being an aggregate, it has no non-static data members of
type pointer to member, non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment operator
and no user-defined destructor, then it is a "POD-struct" [2].
Here "POD" stands for "plain old data".

If it is not a POD-struct, then the contents of each object are
pretty much entirely implementation-dependent.

If it is a POD-struct, then there are rules about layout compatibility [3]
which constrain the representation quite a bit.

> In particular I'm thinking of member functions.  Data members
>are unique to each object.  But what about member function definitions
>?

For non-POD classes, it is implementation dependent.
Most implementations normally use a single `vtable' pointer (though
sometimes more, in particur in the case of multiple inheritence)
in each object which points to a table of virtual functions.
Non-virtual functions are usually not stored in the object at all.
However, the standard does not guarantee this.

I have heard that some C++ implementations use representations which
are quite different from the usual ones, in order to facilitate
better binary compatibility and cheaper recompilation.  With these
implementations, all non-POD objects have the same size (the compiler
implicitly inserts an extra level of indirection) so that if only
the private part of a class Foo has been modified then you don't need
to recompile the classes that use Foo.

References to the C++ standard:
[1] See 9 - Classes [class], paragraph 4.
[2] See 8.5.1 - Aggregates [dcl.init.aggr], paragraph 1.
[3] See 9.2 - Class members [class.mem], paragraphs 14-17.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.
---
[ 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: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/04/12
Raw View
In article <370D0E48.B858221E@xilinx.com>,
Brendan Cullen  <bcullen@xilinx.com> wrote:
>I have a class. It has data members and only non-static and non-virtual
>functions.  I create 2 objects of that class. What's in each actual
>object ? In particular I'm thinking of member functions.  Data members
>are unique to each object.  But what about member function definitions
>?  Why have more than 1definition of each function ?  This may possibly
>not be defined for the language.  It may be implementation specific.
>I'm curious though !

I would expect that in most implementations, the objects would just contain
the nonstatic data members, with pretty much the same layout as in the
corresponding C struct.  If a class has no virtual member functions, the
compiler should be able to resolve all member functions calls statically,
so no vtab (or analogous mechanism) should be needed.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ 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: stanley@west.sun.com (Stanley Friesen [Contractor])
Date: 1999/04/12
Raw View
In article <370D0E48.B858221E@xilinx.com>,
Brendan Cullen  <bcullen@xilinx.com> wrote:
>Hi folks,
>
>I have a class. It has data members and only non-static and non-virtual
>functions.  I create 2 objects of that class. What's in each actual
>object ? In particular I'm thinking of member functions.  Data members
>are unique to each object.  But what about member function definitions
>? ...

Unless the function is inline (explicitly or implicitly due to being
defined in the class definition), there is only one definition of each.
---
[ 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: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/12
Raw View
On 08 Apr 99 22:29:41 GMT, Brendan Cullen <bcullen@xilinx.com> wrote:

>I have a class. It has data members and only non-static and non-virtual
>functions.  I create 2 objects of that class. What's in each actual
>object ? In particular I'm thinking of member functions.  Data members
>are unique to each object.  But what about member function definitions
>?  Why have more than 1definition of each function ?  This may possibly
>not be defined for the language.  It may be implementation specific.
>I'm curious though !

In each object is just the data members.
There may be unused bytes for padding too.
If there are any virtual functions, there is a virtual pointer too.

Member functions and static functions take no space, even if they
are inline!  Here's why:  When you call a member function x.f(),
the compiler implicitly calls a non-member function f(...) that
takes an "X *" as its first argument.  Eg,

   // original
   struct X {
      const int x;
      explicit X(int x) : x(x) { }
      int f(int i) const { return i+x; }
   };
   int main() { X x(1); cout << x.f(3); }

   // how the compiler sees it
   struct X { const int x; }
   void X_init(X *const this, int x) : this->x(x) { }
   int f(X *const this, int i) const { return i+this->x; }
   int main() { X x; X_init(&x,1); cout << f(&x,3); }

--
----------------------------------
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: Brendan Cullen <bcullen@xilinx.com>
Date: 1999/04/08
Raw View
Hi folks,

I have a class. It has data members and only non-static and non-virtual
functions.  I create 2 objects of that class. What's in each actual
object ? In particular I'm thinking of member functions.  Data members
are unique to each object.  But what about member function definitions
?  Why have more than 1definition of each function ?  This may possibly
not be defined for the language.  It may be implementation specific.
I'm curious though !

Regards,

Brendan
---
[ 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/04/10
Raw View
Brendan Cullen wrote:
>
> Hi folks,
>
> I have a class. It has data members and only non-static and non-virtual
> functions.  I create 2 objects of that class. What's in each actual
> object ? In particular I'm thinking of member functions.  Data members
> are unique to each object.  But what about member function definitions
> ?  Why have more than 1definition of each function ?  This may possibly
> not be defined for the language.  It may be implementation specific.
> I'm curious though !

Member functions are not stored with each object. For non-polymorphic
classes like yours, the compiler knows which functions to call, based
upon the static type of the object. The only things actually stored in
your objects are likely to be the non-static data members of the 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              ]