[Mono-devel-list] about Metada

Paolo Molaro lupus at ximian.com
Mon Aug 4 07:09:12 EDT 2003


On 08/04/03 Fernando Diaz wrote:
> I have the Class Prueba with three atributes, the source code is:
> 
> class Prueba{
> 	private string texto;
> 	private Prueba puntero;	
>         private int entero;
>         
>         public Prueba(string texto,Prueba puntero,int entero){
> 		this.texto=texto;
> 		this.puntero=puntero;
>                 this.entero=entero;
>         }
> }
> 
> If i examine the memory to look for this atributes (it would be after de
> object header) i get this (look the picture). The data isn't after the
> object header and the data isn't static.

The data is not static because all the three field are instance fields,
not static fields. And from the picture, you're not looking at the data
after the object header, but at some random MonoClass field.
I'll try to explain it again with different words, but I suggest you
read a good book on computer architectures and C. Someone with better
graphics skills than me may be able to draw a pretty picture of it and
post it somewhere, too.

A C# object in mono is represented as a pointer to a MonoObject structure.
A pointer is simply a memory address, lets say 0x80000000.
The MonoObject structure as you can see in metadata/object.h has two
fields (two pointers) that are stored in memory (on a 32 bit system)
at address 0x80000000 (vtable) and 0x80000000 + 4 (synchronisation).
The MonoObject structure is also called object header, because all the
objects have it at the start of the memory allocated for them.
After the object header, the instance fields for the objects are stored,
in our case, starting from the address 0x80000000 + 8 (8 being the size
of the object header, or sizeof (MonoObject) in C code).
If the object at the address 0x80000000 is a Prueba, then, you'll find
out that the three fields are at offset 8, 12 and 16 from the address
exactly like inspecting the MonoClassField offset field for them will
tell you.
Now, you can get the address of a field. To know the value, you'll need
to dereference the address. For an integer, the C code will look like
this:

	MonoClassField *prueba_entero_classfield = ...get it from MonoClass;
	gint32 *prueba_entero_address = (char*)prueba_obj + prueba_entero_classfield->offset;
	gint32 prueba_entero_value = *prueba_entero_address;

Hope this helps (and hope we're done with basic C lessons on
mono-devel-list:-).

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list