Solved

memory layout and implementation of c++ objects

Posted on 2000-03-02
9
387 Views
Last Modified: 2010-04-02
Can you explain to me about the memory layout and implementation of c++ objects at runtime ?
0
Comment
Question by:Danieldef
  • 7
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 300 total points
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Does that answer your question?  If no can you be more specific?
0
 
LVL 7

Expert Comment

by:KangaRoo
Comment Utility
Why do you want to know? Is there some 'implementation' for which you need to know this?
0
 

Author Comment

by:Danieldef
Comment Utility
Thank you for giving me an answer. If I have any doubts in the near future on this may I ask you for??  
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now