I was wondering – how are objects stored int the memory?
I already know that there’s a reference (like a pointer) that is an int value containing the place in the memory where that object is stored – but how exactly is it stored?
Lets say I have ClassC that has int a, char b, long c, float d and short e – I know how each is stored each have their own set of bit but the amount of bits in each different – so how exactly does C# knows which bits should it access when I write OBJ.c or OBJ.b ?
Is it somewhat possible to convert a whole (nonstatic) object to an array of bytes\bit AND back?
OBVIOUSLY – If there’s something like ClassC.TcpListener tcp – it’s a reference and stored as an int (? Atleast it has the same size in bytes) – but still, how can it different between an int and a pointer?
I was wondering – how are objects stored int the memory? I already know
Share
Generally, this isn’t something you need to care about unless you’re P/Invoking a C library.
It depends on the StructLayout of the object. There are two possible LayoutKinds.
The default is Sequential, where fields are stored in the same order they are declared, aligned appropriately. By default, a
structcontaining your five fields would be stored as:int achar blong cfloat dshort eA
classmay have a slightly different layout due to vtable pointers, garbage collector metadata, etc. I’m not sure exactly how it works.The other LayoutKind is Explicit, where you specify the field offsets explicitly.
Yes, the Marshal class will let you do this. See StructureToPtr and PtrToStructure.
Pointers are not stored as
ints. That wouldn’t work on 64-bit systems. IntPtr is used instead.At which level? C#? CIL? Machine language?