kevinc has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am use embed Perl in c to execute Perl script using perlembed. I have a large c array (double), which I want to pass into a Perl script for processing. Currently I have to create a AV* and pass reference to Perl:
double d[100000]; AV* a = newAV(); for(i=0;i<len;i++) av_push(a, newSVnv(d[i]));

The problem with this, I think, is I have copied whole array. So my question is: is there a way to pass the array directly to Perl without coping? Thanks

Replies are listed 'Best First'.
Re: embed perl: Can I avoid to copy a c array?
by JavaFan (Canon) on Sep 24, 2008 at 15:18 UTC
    As pointed out, the answer is no.

    The reason the answer is no is that in Perl values are much more complex than in C. In C, doubles take 4 or 8 bytes, depending on the platform, and they just have the numerical information. In C, the array is nothing more than all the doubles put sequentially in memory. In Perl, a double will be stored in a C struct, with one its field containing the numerical value. The array will contain pointers to SVs, which themselves are C structs as well, one of its fields a pointer to the struct containing the numerical value. So, in C, an array element can be referenced with just a single offset from the C pointer to the array; in Perl it takes an extra two pointer dereferences (and offsets).

Re: embed perl: Can I avoid to copy a c array?
by jettero (Monsignor) on Sep 24, 2008 at 14:53 UTC
    No.

    Well, perhaps. You could probably pass a tied array back to perl and do the lookups from XS. It feels pretty complicated though, and you'll end up doing all the copying anyway, just slower.

    -Paul

      The tied array method may be useful if you only have to access a fraction of the array; but then you are probably better off by just copying said elements to Perl space. Or you may want to go this route is you just don't have the memory to copy the entire array to Perl space.