Discussion:
Array reference
(too old to reply)
Jeffrey Ratcliffe
2008-10-25 22:00:18 UTC
Permalink
vector_length = av_len(array) + 1;
you need
vector_length = av_len(SvRV(array)) + 1;
Doh! Sometimes you just can't see the wood for the trees.

Thanks!

Jeff
Steve Fink
2008-10-26 05:34:08 UTC
Permalink
if (SvROK(array) && SvTYPE(SvRV(array)) == SVt_PVAV)
printf("array reference\n");
That part is good; you're dereferencing. The rest isn't, because you aren't.

You want something like:

void print_array(SV* array_sv) {
...

if (! (SvROK(array) && SvTYPE(SvRV(array)) == SVt_PVAV)) {
croak("bad arguments to print_array: array reference required");
}
AV* array = (AV*) SvRV(array);
...your original code...
}


On Fri, Oct 24, 2008 at 12:03 PM, Jeffrey Ratcliffe
I seem to be having trouble with an array reference in my xs module.
#!/usr/bin/perl
use Inline C;
__END__
__C__
void print_array(SV * array) {
int i, vector_length = 0;
SV ** svp;
if (SvROK(array) && SvTYPE(SvRV(array)) == SVt_PVAV)
printf("array reference\n");
vector_length = av_len(array) + 1;
printf("vector_length %d\n", vector_length);
for (i = 0; i < vector_length; i++) {
svp = av_fetch(array, i, 0);
if (SvOK(*svp))
printf("element SvOK\n");
printf("element %d: %d\n", i, SvIV(*svp));
}
}
array reference
vector_length 2
element 0: 0
Segmentation fault (core dumped)
Can anyone spot my mistake? Obviously vector_length should be 3, so it
is something fairly fundamental I am missing.
Thanks
Jeff
Jeffrey Ratcliffe
2008-10-26 15:16:31 UTC
Permalink
Post by Steve Fink
That part is good; you're dereferencing. The rest isn't, because you aren't.
Thanks!

Jeff

Loading...