Here you have a snippet:
SV * _smooth2( yp, wp, lambda_in, m ) char * yp char * wp SV * lambda_in int m CODE: { int i, i1, i2, ip; double c[MMAX+1], d[MMAX+1], e[MMAX+1], w[MMAX+1], y[MMAX+1], z[MMAX+1], lambda; double * ya; double * wa; ya = (double *) yp; wa = (double *) wp;
You see, the c uses pointers to address the strings. Char is the default type, you need to convert inside the code.

From perl, I call:

Smooth::_smooth2( $py, $pw, $lambda, scalar( @$yref ) );
Where the $py and $pw are strings. XS automagically builds the pointers for you.

Hope this helps,

Jeroen
"We are not alone"(FZ)
Update: Because the c-code sees only the pointers, no copy is made. To be sure, I tested it with a fresh module: TestMemXS:

#XS #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "unistd.h" MODULE = TestMemXS PACKAGE = TestMemXS void _test( yp ) char * yp CODE: { double * ya; ya = (double *) yp; sleep( 1000 ); } ------------------- #testMemXS.pm .... sub testMem{ my $str = "\000" x 50e6; _test( $str ); } .... ------------------- perl -MTestMemXS -e 'testMem()'
The perl process didn't exceed 55M...

In reply to Re: Passing a very large string by reference to a c library by jeroenes
in thread Passing a very large string by reference to a c library by dino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.