RawMemoryAllocate[type]
分配足够的原始内存来存储指定类型的二进制表示.
RawMemoryAllocate[type,len]
为 len 个对象分配内存.
RawMemoryAllocate
RawMemoryAllocate[type]
分配足够的原始内存来存储指定类型的二进制表示.
RawMemoryAllocate[type,len]
为 len 个对象分配内存.
更多信息
- RawMemoryAllocate 返回托管 RawPointer 对象.
- 可能的类型及其对应的 C 类型包括:
-
"UnsignedInteger8" uint8_t 没有正负号的 8 位整数 "Integer8" int8_t 有正负号的 8 位整数 "UnsignedInteger16" uint16_t 没有正负号的 16 位整数 "Integer16" int16_t 有正负号的 16 位整数 "UnsignedInteger32" uint32_t 没有正负号的 32 位整数 "Integer32" int32_t 有正负号的 32 位整数 "UnsignedInteger64" uint64_t 没有正负号的 64 位整数 "Integer64" int64_t 有正负号的 64 位整数 "CUnsignedChar" unsigned char 与 C 兼容的 unsigned char "CSignedChar" signed char 与 C 兼容的 signed char "CUnsignedShort" unsigned short 与 C 兼容的 unsigned short "CShort" short 与 C 兼容的 short "CUnsignedInt" unsigned int 与 C 兼容的 unsigned int "CInt" int 与 C 兼容的 int "CUnsignedLong" unsigned long 与 C 兼容的 unsigned long "CLong" long 与 C 兼容的 long "CSizeT" size_t 与 C 兼容的 size_t "CFloat" float 与 C 兼容的 float "CDouble" double 与 C 兼容的 double "OpaqueRawPointer" void* 不透明指针 "RawPointer"::[t] t* 类型化指针 {ty1,ty2,…} struct {ty1 f1; ty2 f2; …} 构造或 product 类型 "Void" void 没有输出(仅在输出类型中)
范例
打开所有单元 关闭所有单元基本范例 (2)
为一个类型为 "CInt" 的对象分配内存:
ptr = RawMemoryAllocate["CInt"]RawMemoryWrite[ptr, -1];RawMemoryRead[ptr]为 10 个类型为 "CFloat" 的对象分配原始内存:
buffer = RawMemoryAllocate["CFloat", 10]Do[RawMemoryWrite[buffer, RandomReal[], i], {i, 0, 9}]RawMemoryImport[buffer, {"List", 10}]属性和关系 (3)
默认情况下,RawMemoryAllocate 返回封装在 ManagedObject 中的 RawPointer:
ptr = RawMemoryAllocate["UnsignedInteger8"]如果想解除对指针的托管,可使用 UnmanageObject:
rawPtr = UnmanageObject[ptr]调用 RawMemoryFree 释放已解除托管的指针:
RawMemoryFree[rawPtr]用 RawMemoryWrite 往原始内存中写入一个值:
ptr = RawMemoryAllocate["UnsignedInteger8"];
RawMemoryWrite[ptr, 255];RawMemoryRead 读取存储在原始内存中的值:
RawMemoryRead[ptr]为 10 个 "CLong" 对象分配缓存:
buffer = RawMemoryAllocate["CLong", 10]用 ForeignFunctionLoad 加载填充此缓冲区的函数:
populateArray = ForeignFunctionLoad["compilerDemoBase", "populateArray", {"RawPointer"::["CLong"], "CLong"} -> "CInt"]populateArray[buffer, 10];用 RawMemoryImport 将原始内存导入为列表:
RawMemoryImport[buffer, {"List", 10}]可能存在的问题 (2)
如果碰到不支持的类型,RawMemoryAllocate 将返回 $Failed:
RawMemoryAllocate["ByteArray"]RawMemoryAllocate["UnsignedInteger8", 0]技术笔记
相关指南
-
▪
- 外部函数接口 ▪
- C/C++ 语言接口 ▪
- 调用外部程序 ▪
- 外部操作
文本
Wolfram Research (2023),RawMemoryAllocate,Wolfram 语言函数,https://reference.wolfram.com/language/ref/RawMemoryAllocate.html.
CMS
Wolfram 语言. 2023. "RawMemoryAllocate." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/RawMemoryAllocate.html.
APA
Wolfram 语言. (2023). RawMemoryAllocate. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/RawMemoryAllocate.html 年
BibTeX
@misc{reference.wolfram_2026_rawmemoryallocate, author="Wolfram Research", title="{RawMemoryAllocate}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/RawMemoryAllocate.html}", note=[Accessed: 03-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_rawmemoryallocate, organization={Wolfram Research}, title={RawMemoryAllocate}, year={2023}, url={https://reference.wolfram.com/language/ref/RawMemoryAllocate.html}, note=[Accessed: 03-November-2026]}