1

As far as I know, object variables point to/reference the beginning of their respective object addresses.

class Foo
{
    int a;
    int b;
}

Foo foo = new Foo();

In the above code, foo would point to an instance of Foo created by using new operator, and if the address of that instance was at 0x0010, the address of field a (or maybe b) would also be 0x0010. Similarly, if I created an array of integers and the array object was created at 0x0020, the element at index 0 would share the same address (the indexes are just offsets, right?). However, after reading this article (https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/may/net-framework-internals-how-the-clr-creates-runtime-objects#S7), it seems that, at least in C#, 4 bytes are allocated for TypeHandle at the beginning of every object. If true, then does the first element of every array really start at object address + 4?

Jae Yi
  • 29
  • 2
  • 2
    https://stackoverflow.com/questions/487202/memory-layout-of-a-net-array note that you are asking about the non-guaranteed internals of a particular CLR implementation – pm100 Jan 14 '22 at 00:10
  • 3
    Ultimately, this is implementation specific. .NET does not require a specific physical memory layout. It's possible that different versions of .NET and other current or future implementations use a different strategy to store arrays. – Eric J. Jan 14 '22 at 00:12
  • 2
    Indeed, it's nice to know how it works under the hood, but you should never *rely* on it. A conforming implementation of .NET might hold arrays on the stack, it might have a completely different layout, it might even implement the whole thing on punch cards, or pen and paper. The array might not even exist at all (having been optimized away) – Charlieface Jan 14 '22 at 00:16
  • You would want to specify `[StructLayout]` attribute to have a defined layout in a struct. – John Alexiou Jan 14 '22 at 00:21
  • @pm100 Thanks for sharing that link! I got really valuable information from there, and it seems that the first element of an array does get stored 4 bytes from the beginning of the array instance to account for the overhead – Jae Yi Jan 14 '22 at 00:43
  • @EricJ. Understood. I had been under the impression that the index of an array was a hard value indicating the offset, but it seems that it is implementation specific and that array[0] does not necessarily live at the beginning of the array instance memory – Jae Yi Jan 14 '22 at 00:46
  • @JaeYi that link shows offset 8 to first element . 4 bytes method table pointer, 4 bytes array length – pm100 Jan 14 '22 at 01:58
  • @pm100 I misread that--thanks for pointing that out for me! – Jae Yi Jan 14 '22 at 02:05
  • @Charlieface 'pen and paper' I think you have cracked why my latest c# app is so slow – pm100 Jan 14 '22 at 05:26
  • @pm100 You missed the fact that my punch cards are situated on the other side of the globe and all communication is by [the IPoAC protocol](https://en.wikipedia.org/wiki/IP_over_Avian_Carriers), and given the latency involved and TCP's inherent latency-bound issues, that can also cause slowness – Charlieface Jan 14 '22 at 09:43

0 Answers0