I'm working on an XS perl module and having troubles with an SV. From perlapi, SvPV should coerce any scalar to a string, but whenever I do this, I get a SegFault. Trying example #2 for Inline::C gives me the same. Here's some basic functions I've been testing:
void foo(SV * sv) { if(sv) { printf("%s\n", SvPV_nolen(sv)); } } void bar(SV * sv) { printf ("%i\n", SvPOK(sv)); } Mytest2::foo('asdf'); # prints asdf Mytest2::bar(1); # prints 0 Mytest2::foo(1); # Segmentation Fault
Do I really have to do something like this:
void foo(SV * sv) { char buf[1024]; switch(SvTYPE(sv)) { case SVt_IV: snprintf(buf, sizeof(buf), "%s\n", SvPV(sv)); break; case SVt_PV: snprintf(buf, sizeof(buf), "%d\n", SvIV(sv)); break; case SVt_NV: snprintf(buf, sizeof(buf), "%f\n", SvNV(sv)); break; ... handle scalar refs here ... } printf("%s\n", buf); }
This is on perl 5.14.2, linux x86-64

In reply to SvPV Segmentation Fault by adler187

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.