When you use av_make perl copies the scalar data into the new (Perl, not C) array so you should free the temporary C array you have created. You should instead use newAV and av_store when you do not already have a C array of SV *'s handy.

Perl uses a stack to pass a list of scalars to your subroutine and returns a list of scalars using this stack. When you assign a sub's return value to a perl array, it is stuffing this list into your new array. However it is actually not possible to return a raw array.

The stack can only returns scalars which is why Inline::C automatically returns a reference to a hash or array for you. This also screws up your reference counts. You could use sv_2mortal to properly decrement the reference count in a delayed manner. Read about it more in perlguts for the long explanation.

To return the list you want, you can push values onto the return stack like cdarke has mentioned. When using XS you use XPUSHx to push to the stack directly. For Inline::C it looks like you would use Inline_Stack_Push and the other Inline_Stack macros instead.

Other than the relevant perl manpages, this is the only webpage on XS that helped me any: http://world.std.com/~swmcd/steven/perl/pm/xs/intro/. I haven't used Inline::C very much but I hope that helps.


In reply to Re: A few very basic questions about Extending Perl by juster
in thread A few very basic questions about Extending Perl by unlinker

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.