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