Click to See Complete Forum and Search --> : What's in those objects ?


Brendan Cullen
April 8th, 1999, 12:39 PM
Hi,

I have a class called a_class. I have 2 objects of that class. What's in each actual object ? Is it the data members plus some pointer to where the member functions are defined ? In other words, where in memory are the member function definitions stored ? And is there a definition storage per object ? (Note - I am NOT talking about static member functions.)

Regards,

Brendan

Masaaki
April 8th, 1999, 01:09 PM
Hi.

On the point of UML(Unified Modeling Language) view,
When we declare two objects from one class, each object can access
its data member and its operation. However, each object is different
from the other.
I don't know how these will be allocated in memory. But each member
will de defined by private, protected and public. These can deal with
accessbility from the other class object.

Hope for help.
-Masaaki Onishi-

April 8th, 1999, 01:42 PM
For a class object with no virtual functions, there generally would be no such pointer. For a class object with one or more virtual functions, there would be a "vtable pointer" pointing to the class' virtual function table, in which the addresses of the most-derived versions of the class' virtual functions would be kept -- I believe the vtable pointer is generally the first element in a class objects layout, but I am not sure this is required by the C++ language definition. I do not know how a compiler manages non-virtual member functions, but I think it is implementation-specific and not dictated by the C++ language definition.

Thus, generally, the size of a class object with no virtual functions would be equal to the sum of the sizes of its data members, plus any padding necessary for "alignment" of these data members on word/byte/etc. boundaries -- for instance, a class with two int data members may have a size of 8 bytes if the implementation requires data to be aligned on 32-bit word boundaries, even if each int was only 2 bytes long. I believe the C++ langugage definition (available in the Stroustroup and Ellis book and in the annex to the second edition (but not the 3rd) of The C++ Programming Language describes the requirements in this regard and also describes what is permitted to be implementation-dependent. I highly recommend checking these books out for definitive answers.

By the way, I think many people's attitude would be that a programmer should not need to know much about the layout of class objects and, furthermore, should avoid programming techniques that rely on assumptions about object layout (particularly implementation-specific assumptions). On the other hand, Microsoft uses a number of object-layout assumptions in its implementation of COM.

Remek Zajac
April 8th, 1999, 02:16 PM
Well, i didn't try to implement an object-class structure in memory, but if i had to, i would do it your way.

I had the same problem in mind when implementing an object list. When dealing with objects that offer a variery of operations you're unconsciously affraid that the definitions of these operations may harm your memory space (the more objects the more you're "scarred").
Reasonably: i find the idea of multiplying function definitions with every object of a given class as REDICULOUS. I will stick by that until someone wiser tells me otherwise - and i'll change my profession right after.

You can consider this post as a vote.
"one function definition for multiple objects".

I hope Your question was not like:
Where EXACTLY in memory can the mentioned definitions be found?

Rem
:)

Oak
April 9th, 1999, 05:26 AM
When u define class u define some member functions in it.
There is only one copy of this functions in memory and when
u are calling member function from ur class for ex

a_class.open();

actualy system calls function "open" of ur class and sends it a
pointer to ur object. By using this pointer function can modify data
in ur object.

Brendan Cullen
April 9th, 1999, 05:53 AM
Hi,

So, if there is only one copy of the function in memory, then how does the linker know where it is ?

When is the code for the non-virtual functions set aside ? And how does the linker make the connection between that code and source code that calls such functions ? I.e., how does the linker deal with ;

object_instance.non_virtual_and_non_static_function();

First theory : By deduction it might seem that the compiler sets aside code for the member function the first time there is an object of that class created. But such an instantiation might only be dynamic, ie instantiation taking place at run-time. If that were the case, then the linker would complain. That puts paid to theory 1.

Second theory : The compiler sets aside code for the member function when the class declaration is included. But the function may not even have been implemented at the time of the class declaration/definition. That puts paid to theory 2.

Third theory : The compiler records the name and type of a member function when the class declaration is included. Then at link time the linker searches for the function implementation. How does the linker know where to look for the function ?

Regards,

Brendan

Oak
April 9th, 1999, 06:18 AM
Look at the samples

class a_class
{
int b;
void open()
{
b = 5;
}
}

void main(void)
{
a_class f;
f.open();
}

this code is equvalent to

void open(a_struct *s)
{
s->b = 5;
}

struct a_class
{
int b;
}

void main(void)
{
a_class f;
open(&f);
}

so compiler puts code of the function aside!
BUT it creates function code only in the case
there is explicit call of this member function in source code (so its not run-time operation) In my sample after finding srting "f.open()" compiler will create function open.

Dave Lorde
April 9th, 1999, 09:08 AM
Bear in mind also that an instance of a class with no data members and no virtual functions will not have zero size, contrary to what you might expect, because it is required to have a unique address in memory.

The position of the VFT pointer is not mandated by the standard.

Dave