in reply to What difference between malloc and Newx, how attach a C string to SV directly?

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

Replies are listed 'Best First'.
Re^2: What difference between malloc and Newx, how attach a C string to SV directly?
by Anonymous Monk on Sep 29, 2019 at 09:11 UTC
    #!/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){ printf("SV* x is %p \n", x); SvUTF8_off(x); SvGROW(x,8); SvCUR_set(x, 8); printf("SvPV(x) is p(%p) \n", SvPV_nolen(x)); printf("SvPV(x) is s(%s) \n", SvPV_nolen(x)); char*buff = SvPV_nolen(x); Copy( "dddddddd", buff , 8, char); printf("SvPV(x) is p(%p) \n", SvPV_nolen(x)); printf("SvPV(x) is s(%s) \n", SvPV_nolen(x)); printf("string buff is p(%p) \n", buff); printf("string buff is s(%s) \n", buff); 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 = 0xb2ac54 ""\0 CUR = 0 LEN = 12 SV* x is 0099BCA4 SvPV(x) is p(00B2AC54) SvPV(x) is s() SvPV(x) is p(00B2AC54) SvPV(x) is s(ddddddddng) string buff is p(00B2AC54) string buff is s(ddddddddng) SV = PV(0x3f7a8c) at 0x99bca4 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0xb2ac54 "dddddddd" CUR = 8 LEN = 12