On Thu, Oct 13, 2005 at 02:08:11PM +0200 Reinhard Pagitsch wrote:

> I tryed the following in my XS module:
> 
> int Write1Byte(PerlIO* fh, long what)
> {
>       return PerlIO_write(fh, what, 1);
>       // or return PerlIO_write(fh, (void*)what, 1);
> }
> With this I got a crash.

No wonder. It's horribly wrong. PerlIO_write receives a memory address
as second argument. When you pass it 'what', which is a long, this is
interpreted as a memory address. Is that really what you want? I assume
you want:

    return PerlIO_write(fh, (void*)&what, 1);

But this is then byte-order dependant: It will write the
least-significant byte on little-endian and most significant byte on
big-endian.

> if I do this it works:
> int Write1Byte(PerlIO* fh, long what)
> {
>       int i;
>       char buf[1] = { '\0' };
>       buf[0] = (char)what;
>       i = PerlIO_write(fh, buf, 1);
>       return i;
> }
>
> Can anyone tell me why?

I don't understand the purpose of Write1Byte(). Why is the second
argument a 'long'? Can you explain the semantics of this function first?
Is it supposed to write the least significant byte of an integer value?

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);

Reply via email to