[Mono-devel-list] Creating a string[] from C

Martin Baulig martin at ximian.com
Fri Feb 6 08:33:40 EST 2004


On Thu, 2004-02-05 at 18:53, James Lamanna wrote:
> The C code that I'm using to generate the array looks something like this:
> static MonoArray *generateArrayFromC()
> {
>      MonoString *test_string = mono_string_new(domain, "Test String");
>      MonoArray *ip_array;
>      int elem_size;
> 
>      ip_array = mono_array_new(domain, mono_defaults.string_class, 1);
>      elem_size = mono_array_element_size(ip_array->obj.vtable->klass);
>      memcpy(mono_array_addr_with_size(ip_array, elem_size, 0), test_string, 
> elem_size);
> 
>      return ip_array;
> }

Hello,

this code is wrong; string is a reference type, not a value type.  This
means that your array just holds pointers to the string, ie. you do
something like

   test_string = mono_string_new (domain, "Test String");
   ip_array = mono_array_new (domain, mono_defaults.string_class, 1);
   mono_array_set (ip_array, gpointer, 0, test_string);

--
Martin Baulig <martin at ximian.com>
Now blogging !  http://primates.ximian.com/~martin/blog




More information about the Mono-devel-list mailing list