How are C++ objects laid out in memory ?



Hi hackers,

I'm investigating on how C++ objects can be accessed and invoked by
the external code (e.g. a C code, or a assembly language routine, or
some other language routines). I'm using "Microsoft 32-bit C/C++
Optimizing Compiler v. 13.10.3052". How C++ class is actually
laid out in memory ?

My half correct guess is representation as a structure is
represented. e.g.

class Msg
{
char* msg;
public:
Msg(const char*);
void print();
~Msg();
};

might be represented in C as:

struct MsgStruct
{
char* msg;

void (*construct)(struct MsgStruct*, const char*);
void (*print)(struct MsgStruct*);
void (*destruct)(struct MsgStruct*);
};

But the function pointers declared in above MsgStruct structure have to
be invoked using "thiscall" calling convention (documented in MSDN,
where "this" pointer is passed in ECX register), and "thiscall"
convention can't be explicitly. So a tweak will be needed as below:

/* Invoke method on Msg object not MsgStruct stucture */

void invoke_print_method(void* p)
{
Msg* m = (Msg*)p; /* Cast a Msg object from parameter */
void (Msg::*fn)() = &Msg::print;
unsigned** px = (unsigned**)(&fn);

__asm {
lea eax, [fn] ; Get the value of pointer, i.e. address of print()
mov ecx, [m] ; Now, set this pointer
call [eax] ; Invoke the function, since EAX contains address
; of print() method
}
}

But some of my thoughts contradicts what I've actually derived
above. That's why I've not used If we've to represent C++ member
methods as the function
pointers in C structure, then this means we've to duplicate function
pointers for each object which also leads to memory wastage. And this
means, size of C++ object is increased. But size of C++ object remains
4 bytes, whereas size of structure instance is 16 bytes (4 bytes data,
12 bytes for 3 function pointers).

Suppose I want to expose a C++ object to some C code, although that C
code can cast my C++ object to a pointer and can change its data, but
what about member methods. And is there any standard that controls
this behavior ? Or every compiler does in its own way ? Then how
member methods can be invoked ? Is there any table of function
pointers which I can locate and then invoke the function pointers ?

And by the way, how COM does it ?

Thanx in advance,

Ashish Shukla alias Wah Java !!
Wah Java !!

-----------------------------------

tsorF treboR - peels I erofeb og ot seliM

.



Relevant Pages

  • Re: How are C++ objects laid out in memory ?
    ... If a C++ class or struct contains no virtual methods, ... There are no function pointers. ... >pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)
  • Re: How are C++ objects laid out in memory ?
    ... > void print(); ... int main ... > But the function pointers declared in above MsgStruct structure have ... > pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)
  • Re: virtual functions vs speed
    ... > I have been thinking about using function pointers but that seems as ... Virtual functions work via pointers (just to cover my bases here: ... ever played the game Mortal Kombat? ... there's a character in it called Shang Tsung who can morph into every other ...
    (comp.lang.cpp)
  • Re: Dynamic linking with symbols
    ... Thus I need to either pass all the singeltons pointers or pass function pointers to functions hooking the callbacks into the singeltons. ... plugin writer to use a c style interface. ...
    (microsoft.public.vc.language)
  • Re: Indirect method calls ... how to?
    ... Delegates are "function pointers" in .NET. ... > class and then call the Invoke() method of it to actually execute it. ... Depending on a configuration ... >> public bool Add ...
    (microsoft.public.dotnet.languages.csharp)