I have merrily allocated structures without a care to deallocate the spaceIf you allocate any memory using malloc then perl will not deallocate it for you, you have to free the structures yourself.
In the case of a string, why do I not get a reference to a perl scalarPerl references are not the same as C pointers.
newSVpv creates a perl scalar with the value of a string and returns a pointer to it, not a reference. To create a reference use
newRV or
newRV_inc.
Although
Data::Dumper is very useful, for looking at the details of variables created in this way
Devel::Peek is invaluable, for example:
use Devel::Peek;
print mkstr() . "\n";
my $str = mkstr();
print Dump($str),"\n\n";
my $arr = mkarr();
print ref($arr) . "\n";
print Dump($arr) . "\n";
__END__
__C__
... your code ...
Gives (on Linux 5.12.0):
This is another string!
SV = PV(0x8e2f128) at 0x8cebf60
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x8dade30 "This is another string!"\0
CUR = 23
LEN = 24
ARRAY
SV = IV(0x8cebfcc) at 0x8cebfd0
REFCNT = 1
FLAGS = (PADMY,ROK)
RV = 0x8e51cb0
SV = PVAV(0x8d55470) at 0x8e51cb0
REFCNT = 2
FLAGS = ()
ARRAY = 0x8e40b10
FILL = 9
MAX = 9
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 0
SV = IV(0x8e1d3f4) at 0x8e1d3f8
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 0
Elt No. 1
SV = IV(0x8e1d4a4) at 0x8e1d4a8
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 1
Elt No. 2
SV = IV(0x8e1d3a4) at 0x8e1d3a8
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 2
Elt No. 3
SV = IV(0x8d9971c) at 0x8d99720
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 3
Use of uninitialized value in concatenation (.) or string at ./gash.pl
+ line 18.
This clearly shows that your mkstr() function returns a scalar value as a string, and it looks like there is a problem with the memory allocation in mk_arr().
Personally I do not create Perl arrays in my XS code, I return lists instead, and then the caller can stuff the list where it wants (I don't think
av_make() is a common way of creating a Perl array). Typically I would loop creating the list elements on the stack using:
XPUSHs(sv_2mortal(newSVpvn (szString, iLen)));
Finally, I recommend "Extending and Embedding Perl" by Tim Jenness and Simon Cozens (Manning).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.