tags:

views:

581

answers:

6

Hi,

I am basically wondering how C++ lays out the object in memory. So, I hear that dynamic casts simply asjusts the object's pointer in memory with an offset; and reinterpret kind of allows us to do anything with this pointer. I dont really understand this. Details would be appreciated!

Thanks.

A: 

This looks like a good start, although the Question could probably be clearer.

Amit
+1  A: 

If you really want to go in depth this is the best book: Inside C++ object model

Naveen
+1  A: 

The answer is, "it's complicated". Dynamic cast does not simply adjust pointers with an offset; it may actually retrieve internal pointers inside the object in order to do its work. GCC follows an ABI designed for Itanium but implemented more broadly. You can find the gory details here: Itanium C++ ABI.

Hyman Rosen
+2  A: 

Each class lays out its data members in the order of declaration.
The compiler is allowed to place padding between members to make access effecient (but it is not allowed to re-order).

How dynamic_cast<> works is a compiler implementation detail and not defined by the standard. It will all depend on the ABI used by the compiler.

reinterpret_cast<> works by just changing the type of the object. The only thing that you can gurantee that works is that casting a pointer to a void* and back to the same the pointer to class will give you the same pointer.

Martin York
Your first point isn't completely correct. The only guarantee you have is that members in the same access block will have a defined order. If you wanted to take this to its extreme, you could say that even where the access is the same that the order is no longer guaranteed.
Richard Corden
@Richard. I am not sure I understand you. The compiler is not allowed to re-order elements (this is for back wards compatability with C). What is an access block. Can you point me at the correct part of the standard that you are getting your information from?
Martin York
@MartinAn access block (really, access specifier) is the `public:`, `private:`, and `protected:` in a class.I found this article: http://www.embedded.com/design/218600150?pgno=1 extremely useful, and that's where I first learned what Richard mentioned in his comment. I looked up the C++ standard linked there, and on pg. 198 (sec. 9.2 clause 12) it states:"The order of allocation of non-static data members with different access control is unspecified"
Allen George
+1  A: 

See this article for quite comprehensive description of how it's done inside the MSVC compiler.

arul
A: 

As stated previously, the full details are complicated, painful to read, and really only useful to compiler developers, and varies between compilers. Basically, each object contains the following (usually laid out in this order):

  1. Runtime type information
  2. Non-Virtual base objects and their data (probably in order of declaration).
  3. Member variables
  4. Virtual base objects and their data (Probably in some DFS tree search order).

These pieces of data may or may not be padded to make memory alignment easier etc. Hidden in the runtime type information is stuff about the type, v-tables for virtual parent classes etc, all of which is compiler specific.

When it comes to casts, reinterpret_cast simply changes the C++ data type of the pointer and does nothing else, so you had better be sure you know what you're doing when you use it, otherwise you're liable to mess things up badly. dynamic_cast does very much the same thing as static_cast (in altering the pointer) except it uses the runtime type information to figure out if it can cast to the given type, and how to do so. Again, all that is compiler specific. Note that you can't dynamic_cast a void* because it needs to know where to find the runtime type information so it can do all its wonderful runtime checks.

Jason E