I would use a subroutine like this:

SV * fill_array(SV* name1, ...) {
    int i;

    Inline_Stack_Vars;

    // find out how many values were passed
    int n = Inline_Stack_Items;

    // allocate memory for the data
    New(0,array,n,double);

    // iterate through the stack and assign it to the new array
    for(i=0;i<Inline_Stack_Items;i++) {
        array[i] = atof(SvPV(Inline_Stack_Item(i),PL_na));
     }

    // return the SV * to the newly allocated array
    return sv_setref_pv(newSViv(0),"x", (void *)array);
 }

Though if someone can tell me a way to eliminate the atof() call, I'd
appreciate it.  I used to do it such that I used the regular C++ new, and
returned a double *, but Perl would not delete the memory cleanly that way.
This creates something PErl is more comfortable with.

Only thin is, when you pass the return value to another C subroutine, you
will need to put a line in like this:

void another_subroutine(SV * p) {
    double * array = (double *) SvIV(SvRV(p));
 }

Then you should be able to use array[n] as normal.  I did this with char and
char * arrays (for images), but it should work for doubles just as well.
You may need to alter the Sv macros a bit, as I'm not too familiar with
them, but stuff like this works fine for me.  Hope this helps.

--
Dave

Reply via email to