Thanks Everyone for your help! After doing some dig with your valuable suggestion, I attach a C string (string constant) to a SV successfully. Here is the UPDATE

When attach an mem-chunk to an SV, perl does much work for you background, not just save a pointer address. First, perl free the previous PV in SV, then perl will realloc this mem size to mem size +2bytes( I don't understand why though), last, perl set cur and len slot and return. So if you want to attach a C string, you must tell perl not doing so many smarts:

use strict; use warnings; use Devel::Peek; my $x = ""; Dump $x; ddd($x); print $x; Dump $x; use Inline Config => BUILD_NOISY => 1, ClEAN_AFTER_BUILD =>0; use Inline 'C' => 'Config' => ccflagsex => "-g"; use Inline 'C' => <<'CODE'; int ddd(SV* x){ char* dx = "dddddd\n"; printf("string dx is %p \n", dx); // You have to use sv_use* family, since these functions free the prev +ious PV in SV first for you, but don't use sv_usepvn otherwise perl w +ill realloc pv. sv_usepvn_flags(x, dx, 8, SV_HAS_TRAILING_NUL); // Thanks ikegami, must set LEN to 0, otherwise, perl will try to free + SV when leave scope. SvLEN_set(x, 0); return 0; } CODE
As a interesting side-effect, I found you can't enlarge the SV containing C string by using .=, no any error throw though.

__END__ SV = PV(0x55ac3017e050) at 0x55ac301a20b8 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x55ac30220b00 ""\0 CUR = 0 LEN = 10 COW_REFCNT = 1 string dx is 0x7f0adfd33bfd dddddd SV = PV(0x55ac3017e050) at 0x55ac301a20b8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x7f0adfd33bfd "dddddd\n\0" CUR = 8 LEN = 0




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction


In reply to Re: What difference between malloc and Newx, how attach a C string to SV directly? by xiaoyafeng
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.