Continued, previous part
hereSo, today we are with a hammer and shovel we will try to
make the craniotomy get into the interior of .NET.
There is a simplest class:
class X {public int Val; } . When creating an object of this class, what real size will it occupy? And what values ​​will contain additional fields? Show proof as a C # code;)
Of course, this is only the first riddle of the series, but this should be enough for at least the weekend. Clarification of .NET internals is quite a big topic.
Note: since these are system-dependent things, I will assume that the code runs on a 32-bit OS.
')
UPD So, two similar solutions were received: from
lam0x86 :
iaroshenko.habrahabr.ru/blog/77275/#comment_2250121 and from
crjd :
iaroshenko.habrahabr.ru/blog/77275/#comment_2252317 . I first classified them as incorrect, but later I saw my mistake.
However, both decisions are based on the fact that the class has an open field
int
. If this is not a field, but a property, or there are no fields or properties at all, then this solution will not work.
Therefore, I decided to demonstrate the “author's version”:
iaroshenko.habrahabr.ru/blog/77275/#comment_2269072 . Naturally, it will work only within this limited framework: the simplest program, where there is no need to create other objects or collect garbage.
By the way, if you remove the fields completely, the size still remains the same, only the additional field is zero.