Hi,

The reason you get a segmentation fault is because when ddd() ends char* dx gets freed

I think the idea is to use SvPV to get a char* to give to your C function, something like this but by a real c-programmer :)

I couldn't get it to compile using SvPV or SvPVX calls, dont understand why , error is

error: lvalue required as left operand of assignment error: lvalue required as unary '&' operand

This compiles but I couldn't make this assign ddddd string , needs c-programmer

#!/usr/bin/perl -- use strict; use warnings; use Inline C => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline 'C' => <<'CODE', NAME => 'xiaoyafeng'; int ddd(SV* x){ SvUTF8_off(x); SvGROW(x,10); char*buff = SvPV_nolen(x); *buff = "dddddd\n"; printf("SV* x is %p \n", x); printf("SvPV(x) is %p \n", SvPV_nolen(x)); printf("string buff is %p \n", buff); SvCUR_set(x, 10); SvSETMAGIC(x); return 0; } CODE use Devel::Peek qw/ Dump /; my $x = ""; Dump $x; ddd($x); Dump $x; exit( 0 ); __END__ SV = PV(0x3f7a8c) at 0x99bca4 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0xf46a04 ""\0 CUR = 0 LEN = 12 SV* x is 0099BCA4 SvPV(x) is 00F46A04 string buff is 00F46A04 SV = PV(0x3f7a8c) at 0x99bca4 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0xf46a04 "$\0matched\0" CUR = 10 LEN = 12

In reply to Re: What difference between malloc and Newx, how attach a C string to SV directly? by Anonymous Monk
in thread What difference between malloc and Newx, how attach a C string to SV directly? by xiaoyafeng

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.