perlxs

Check if array elements are undefined from an XSUB


I am trying to check if an array element is undef from an XSUB like this:

void
print_array(array)
    AV *array

    PREINIT:
        int len;
        SV **sv_ptr;
        SV *sv;
        int i;

    CODE:
        len = av_len(array) + 1;
        printf("[");
        for (i = 0; i < len; i++) {
            sv_ptr = av_fetch( array, i, 0 );
            if (!sv_ptr) {
                printf("empty");
            }
            else {
                sv = *sv_ptr;
                if (sv == &PL_sv_undef) {
                    printf("undef");
                }
                else {
                    printf("*");
                }
            }

            if (i < (len - 1)) {
                printf(", ");
            }
        }

        printf("]\n");

If I run this sub from a Perl script:

use strict;
use warnings;
use ArrayPrint;

my $array = [];
$array->[4] = undef;
ArrayPrint::print_array($array);

The output is:

[empty, empty, empty, empty, *]

Why is the last element not showing undef?


Solution

  • An SV can hold an undefined value but still be a different SV than PL_sv_undef. You need to replace the PL_sv_undef test with

    SvGETMAGIC(sv);
    if (!SvOK(sv)) { printf "undef" } else { printf "*" }