Page 1 of 1

PostPosted: Mon Jan 23, 2006 3:39 am
by mgreen
Please come up with some simple examples of native add-ins handling of arrays. Are arrays implemented as vectors of pointers to Values or as vectors of Values?

PostPosted: Thu Jan 26, 2006 2:22 am
by mgreen
There's an example in the description of the VM's handling of memory of access to the fields of a structure or object. Does the same apply to arrays? In particular

Value* optr = ofi->vm_pop(); // pointer to return struct
Value* o_x = ofi->vm_deref(optr->iVal); // dereference the struct pointer to
Value* o_y = ofi->vm_deref(optr->iVal + 1); // get pointers to the two fields

provides for access to two fields of a structure result and, I assume, any structure parameter. What about arrays? Does it work the same? Is there any way to find out the number of elements allocated in the array like a callback to sizeof? If so, how can I get a copy of orbc.sys for reference for vm_callBI? Is there any way to allocate a string array (or any array) in a native function other than using callbacks? If so, what?

PostPosted: Thu Jan 26, 2006 3:57 am
by dewey
An array is stored in memory as a contiguous set of Values. For the following code:

Code: Select all
void func() {
  int array[5];
  native_func(array); // same as native_func(&array[0])
}

The address of the first element of the array is passed into the native function. The native function would then read the array like this:

Code: Select all
long addr = ofi->vm_pop()->iVal;
Value* element0 = ofi->vm_deref(addr);
Value* element1 = ofi->vm_deref(addr+1);

You can't find out the size of an array, as it isn't known at runtime. You function will have to take size as an argument.

orbc.sys is in \Program Files\OrbForms or PocketC Architect.

To allocate memory, you need to use vm_callBI to call malloct.

PostPosted: Thu Jan 26, 2006 12:56 pm
by mgreen
Thank you. That's very helpful.