Reinhard Pagitsch said: > Hello, > > 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. > > 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?
Because (void*)what is not the same as &what. I assume you're wanting to write value-of-long-what-cast-as-char to the stream. (void*)what tells PerlIO_write() to find the data to write at the address value-of-long-cast-as-memory-address, which isn't what you want at all. The way you did it in your second function, copying the data into a stack buffer, is more work, but will be safer across byte orders than taking the address of the function argument. That is, you don't have to worry about machine byte order messing up whether *(char*)long will be the top byte or bottom byte. -- muppet <scott at asofyet dot org>