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