object layout structure in .NET using disassembler
I am interested in seeing the object layout structure, and am trying to use a disassembly in visual studio. Following is my code:
class myclass
{
public int m_a;
}
myclass myc = new myclass();
myc.m_a = 23;
//I am setting a breakpoint after this line
I opened Memory1 window开发者_如何学Go, and type myc in the Address field. I get the following details int the output window (used Windows XP PC 32bit with Intel compiler):
0x0148B7BC 1c 93 a7 00 17 00 00 00 00 00 00 00 00 00 00 00 00 00 00
It appears that there is an additional pointer 00a7931c which is added in front of the object data, which increases the object size by 4 bytes. My confusion is that documentation says that object size is increase by 8 bytes due to header per object. Can someone please point me to where the other 4 bytes are?
From Advanced .Net Debugging - CLR Object’s Internal Structure:
An object’s CLR internal structure is:
[DWORD: SyncBlock][DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…
Object Header: [DWORD: SyncBlock]
Object Pointer: [DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…Every Object is preceded by an ObjHeader (at a negative offset). The ObjHeader has an index to a SyncBlock.
Take a look at 0x0148B7B8. An objects structure is:
SyncBlock (ptr size) MethodTable (ptr size) Fields...
The reference points to the method table, to allow for faster dereferencing (consider the relative frequency of virtual method and property calls vs. locking).
精彩评论