Link to home
Start Free TrialLog in
Avatar of Danieldef
Danieldef

asked on

memory layout and implementation of c++ objects

Can you explain to me about the memory layout and implementation of c++ objects at runtime ?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

If an object is POD (plain old data--that means that it has not virtual function, no base classes, no user defined constructors and assignment operators, and all of its data members are also POD.  Basically, that means if the object is just like a C struct).  You are also guaranteed that a pointer to the object, will point to the same address as the first data member declared in the object.  

For a POD object you are also guaranteed that two POD objects of different classes but with the same data members in the same order will use the exact same layout in memory.  This guarantee is not made for non-POD classes.

continues
Is that the type of information you are looking for?

Maybe you want something a little more basic?  I'm not sure.  

If you declare a class like

class Cls1
{
   int X1;
   double D1;
   static char C1;
};

Then objects of the class will actually store only two data members, X1 and D1, the C1 data member is static, and is not stored in each object, it is stored statically, like a global variable.  The standard guarantees that the object layout in memory will store x1 at a lower address than D1, but there may be padding between them.  if I had done

class Cls2
{
   int X2;
public:
   double D2;
   static char C2;
};

you would not be guaranted that D2 is stored at a higher address than X1, or that they are even stored consecutively, because of the public label

In either case (Cls1 or Cls2) there may be padding in the form of unused bytes between the X2 and D2 members.

continues
Since all the non-static data members of Cls1 are POD, and it has no user defined constructors, destructors, assigment operator, or virtual functions, Cls1 is a POD class.  This means you are guaranteed that the a pointer to an object of the class points to the X1 data member.  You are not guaranteed this of the Cls2 class because of the public: label.

Now if you add base classes to the equation, there is no guarantee as to how the class will be laid out in memory, it is up to the C++ implimentation you are using, but usually, the object is laid out as an copy of the base class followed by the data members of the derived class.

For example

class Cls1
{
   int X1;
   double D1;
};

class Cls3 : public Cls1
{
   int I3;
}

Cls3 will probably consist of a Cls1 object followed by (moving to higher addresses)   Cls3's data members, so you would usually find the order woudl be X1, D1, I3.  Again there may be padding between these.  (Once again, this is implimentation defined, this is just a typical way to do it.)
If a class contains virtual functions, then the class will probably contain additional data used by the implimentation to allow virtual functions to behave correctly  Usually (again, this is implimentation defined) this will take the form of a virtual function table pointer (often called vtbl)  This is simply a pointer to an array of function pointers.  These function pointers are initialized to point to each of the virtual functions declared in the class.  Each object in a class will share this table of function pointers, that is, each one will have a vtbl pointer that points to the same vtbl.  However objects of different classes will have unique function tables.    So for a class like

class Cls4
{
  int X4;
  virtual void f1();
  virtual void f2();
};

There would be an array defined in memory like

void *vtbl[] = {Cls4::f1,Cls4::f2};

and each object would begin with a pointer to this array followed by its data members (X4).  Again that is all implimentation defined, but this is typical.
Does that answer your question?  If no can you be more specific?
Why do you want to know? Is there some 'implementation' for which you need to know this?
Avatar of Danieldef

ASKER

Thank you for giving me an answer. If I have any doubts in the near future on this may I ask you for??  
Well, "any doubts"?  I am not a certified therapist or anything.  But if you confine them to C++ and programming in general, sure.  :-)