简体   繁体   English

如何为ByteString创建Ptr Word8

[英]How to create a Ptr Word8 for ByteString

create , from Data.ByteString.Internal, states that it needs a Ptr Word8 in order to create a ByteString. 创建 ,从Data.ByteString.Internal,指出它需要一个Ptr Word8以创建一个字节串。 I'm guessing this is like a reference to the head of the bytestring or something. 我猜这就像是对字节串头的引用或其他内容。 However, I'm not sure what I should use to create a new pointer - I'm fairly sure it's not done properly with nullPtr . 但是,我不确定应该使用什么来创建新的指针-我很确定使用nullPtr不能正确完成

No, create gives you a pointer to a memory to fill: 不, create会为您提供指向要填充的内存的指针:

create :: Int -> (Ptr Word8 -> IO ()) -> IO ByteString

The first argument is the length of bytestring to create, the second is a function, that fills the bytestring. 第一个参数是要创建的字节串的长度,第二个参数是填充字节串的函数。 Basically create allocates memory buffer of the specified size, then calls the function with the pointer to the buffer. 基本上, create分配指定大小的内存缓冲区,然后使用指向缓冲区的指针来调用该函数。 Usage example: 用法示例:

> create 5 $ \ptr -> pokeArray ptr [65, 66, 67, 68, 69]
"ABCDE"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM