Is there a way to extract underlying direct pointer to memory off the ByteString object? My current approach is incorrect, compiler says.
getPtr :: ByteString -> Ptr Word8
getPtr (PS ptr _ _) = ptr
Use unsafeUseAsCString
from Data.ByteString.Unsafe
. It has type:
ByteString -> (CString -> IO a) -> IO a
You could use unsafeUseAsCString bs return
to simply get the pointer, but this is highly unsafe because the ByteString
isn’t guaranteed not to move, and the memory may be freed at any point after the CString -> IO a
function terminates, so you should only access the pointer within it; internally it uses Foreign.ForeignPtr.withForeignPtr
, which pins the memory so it won’t move if a GC happens. Per the docs:
The memory may freed at any point after the subcomputation terminates, so the pointer to the storage must not be used after this.
The string also won’t be null-terminated unless the ByteString
happened to be, and it will have type CString
(that is, Ptr CChar
) instead of Ptr Word8
, but you can use castPtr
from Foreign.Ptr
to get around that if it’s a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With