Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

memory layout and implementation of c++ objects

Posted on 2000-03-02
9
Medium Priority
?
439 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 1200 total points
ID: 2578777
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
ID: 2578788
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
ID: 2578798
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:nietod
ID: 2578822
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
 
LVL 22

Expert Comment

by:nietod
ID: 2578831
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
ID: 2578834
Does that answer your question?  If no can you be more specific?
0
 
LVL 7

Expert Comment

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

Author Comment

by:Danieldef
ID: 2580708
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
ID: 2580750
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

824 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