Search code examples
.netclr

CLR class memory layout


What is the memory layout of a CLR class?

Coming from a C++ background, the memory layout of a C++ class with virtual functions starts with a v-table pointer, and then the data members of the class follow in memory.

Do CLR classes with virtual functions have a v-table pointer? Is this pointer the first field in the class memory layout? Are there any extra fields in a CLR class memory layout in addition to programmers' defined data members? And what do these extra fields represent?


Solution

  • It's implementation specific, but this article gives a description of what was present in the Microsoft .NET implementation, for CLR v1.1. I suspect it's the same for CLR v2 and v4, but I couldn't guarantee it. Look for a section called "ObjectInstance" for the details - but you may find the whole article interesting.

    According to the article, there are basically two bits of header: the sync block which is used for locking, and the type reference which is basically a pointer to type information (including the vtable).

    A more recent - but potentially still pre-.NET-core article is here.