http://qs1969.pair.com?node_id=1180092


in reply to Is this a sane/safe way to pass an aref into a C function?

I don't see anything particularly wrong with your approach, but I have a few thoughts:

  1. buf[i] = (int)SvNV(*elem);

    buf[] is define as unsigned char, but you're retrieving reals, and casting them to signed ints?

  2. If you are going to pass in an array of ints or reals, you should really check they will fit into an unsigned char or cast appropriately to ensure they will.

    Maybe that should be buf[i] = (unsigned char)SvNV(*elem); or even buf[i] = (unsigned char)SvUV(*elem);

  3. I'll be passing the char buffer and len to an external C function.

    Perl's string scalars can hold any unsigned char value, and carry their length, so why not just pass the pointer and length from a scalar argument?

    That way you cannot be passed out of range values so don't have to deal with them.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.