Dear Nick,
Nick Ing-Simmons wrote:
Reinhard Pagitsch <[EMAIL PROTECTED]> writes:Hi,
Muppet wrote:
Thank you for the information. What have I to do now?
On Sep 15, 2004, at 11:42 AM, Reinhard Pagitsch wrote:
If I populate an array with av_push will the SV* val a copy in the array or it is only a reference to it?
av_make() copies the svs you give it, but none of the other av functions do. av_store() and av_push() both store in the av the actual pointers you give to them, which means they basically own the svs.
if in doubt, see Perl_av_store() in perl/av.c; near the end, the line ary[key] = val is where the pointer passed as an arg is stored in the internal data structure. val's refcount is never touched.
What ever you like ;-)
That is you can either:
A. Leave it the way it is.
This assumes that previous "owner" of resl SV is not going to free it. As you have done a newSVxxx() in your example your code is the owner and there is no problem.
But if "resl" had been passed to you as a parameter (owned by caller)
or found in another data structure (be that a perl AV or HV or something custom) then you don't own it so one of following is needed.
Ok, I am the owner of "resl" so there is no need to change my code. :-)
Whats about the REFCNT? Can you please explain or give me a link where I can read it?B. Make a copy of each SV as you add it to the array.
if(!check(res, res1)) { av_push(res,newSVsv(res1)); }
C. Adjust its REFCNT
if(!check(res, res1)) { av_push(res,SvREFCNT_inc(res1)); }
The perl documentation is not very clear to me in this and many other cases. :-(
I not want to pass an arry to my XS, I want to get back an array. But I will keep your code in mind for further use.With av_make I have to know how many SV's will be stored.
But I do not know it at this time. Is the way I do it now the correct one?
Depends
BTW is there a XS function like the strtok() in C or the split() in Perl? I did not find one in the Activestate documentation.
XS _is_ C code so you can use C functions.
I tryed the strtok() function but Perl crashes.
strtok() is a tricky thing - raw strtok() uses static data between calls so it thread hostile.
I usually use split() in perl code and then pass results to the XS,
or for simple cases something like:
Thank you.
char *string = ...l char *sep; while ((sep = strchr(string,',')) { process(string,sep-string); string = sep+1; }
mit freundlichen GrÃÃen, with my best regards, Reinhard