develooper Front page | perl.perl6.users | Postings from May 2020

Re: NativeCall questions

Thread Previous | Thread Next
Curt Tilmes
May 8, 2020 12:56
Re: NativeCall questions
Message ID:
On Fri, May 8, 2020 at 8:49 AM David Santiago <> wrote:

> EncodedData* ed = malloc(sizeof(EncodedData));
> ed->data = encbuffer;
> ed->crc32 = crc32;
> return ed;

You're returning a pointer to encbuffer -- make sure the storage for
that is kept around
somewhere.  If it is passed in from Raku, you'll be fine as long as
you hold on to the
object on that side.  Otherwise you might need to allocate/copy it to make sure.

> class EncodedData is repr('CStruct') {
>     has CArray[uint8] $.data;
>     has uint32 $.crc32;
> }

That's fine, but CArray[uint8] is just a pointer -- it doesn't know
how long the array
it is pointing to is.  It either needs a sentinel at the end (like the
Nul at the end of a
C string), or a separate field with a size to figure that out.

> Don't know how many elements a C array returned from a library



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About