[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

memory layout and implementation of c++ objects

Can you explain to me about the memory layout and implementation of c++ objects at runtime ?
0
Danieldef
Asked:
Danieldef
  • 7
1 Solution
 
nietodCommented:
The standard makes very few guarantees about the layout.

You know that data members will appear in the same order they are declared going toward increasing memory address . (This order may be disrupted at any place there is a private: protected: public: label.)

The compiler may place padding between adjacent data members or at the end of the object.

The compiler may include data that is not declared, such as a virtual function table pointer or virtual base class displacements.

continues.
0
 
nietodCommented:
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
0
 
nietodCommented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
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.)
0
 
nietodCommented:
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.
0
 
nietodCommented:
Does that answer your question?  If no can you be more specific?
0
 
KangaRooCommented:
Why do you want to know? Is there some 'implementation' for which you need to know this?
0
 
DanieldefAuthor Commented:
Thank you for giving me an answer. If I have any doubts in the near future on this may I ask you for??  
0
 
nietodCommented:
Well, "any doubts"?  I am not a certified therapist or anything.  But if you confine them to C++ and programming in general, sure.  :-)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now