0

I was reading this article (which is about how to allocate class object in the stack segment) and came across to this snippet. If I understand correctly he wants to get the address of our PocoClass with the header (with lock object and type information).

TypedReference typedReference = __makeref(heapPoco);
int* poco1Address = (int*)(*(int*)(*(int*)(&typedReference)) - 4);

Question

If we know that a class has 2 other "objects/information" before actual data, then why are we subtracting 4 (he's in 32bit mode in his example) from our address, shouldn't it be 2 * 4, because we have a pointer that points to "type object" and before that we have the "lock object".

  • 1
    No, second image in the blog post. An object reference points to the 2nd field in the object header, not the object data. Done to make accessing a boxed value as efficient as possible. – Hans Passant Mar 25 '21 at 07:21
  • @HansPassant Thank you. Now it makes sense. –  Mar 25 '21 at 08:08

0 Answers0