develooper Front page | perl.perl6.users | Postings from December 2019

NativeCall Doc booboo

Thread Next
From:
Todd Chester via perl6-users
Date:
December 31, 2019 04:54
Subject:
NativeCall Doc booboo
Message ID:
5f401123-9223-aea7-6e2d-dc455c8de065@zoho.com
Hi All,

https://docs.raku.org/language/nativecall.html#Passing_and_returning_values

Did anyone else discover the mistake, hopefully not the hard way like I
did.  Anyone get weird gibberish printed out like I did?

    my$string="FOO";
    # The lifetime of this variable must be equal to the required lifetime of
    # the data passed to the C function.
    my$array=CArray[uint8].new($string.encode.list);v

The mistake is that "C" strings are terminated by a chr(0) -- in
"C" terms chr(0) is called a nul or 0x00.

If you don't terminate your C string, the reading functions keep careening
until it finds a chr(0).  You have to tack a chr(0) onto the end.

    my$array=CArray[uint8].new($string.encode.list);
    $array [$array.elems] = 0;

$array should be 70 79 70 0  not  70 79 79
You can test this with `say "$array"` (include the quotes).

Maybe JJ will pick up on this and get it fixed.

-T

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About