RawPointer[addr,type]
represents a raw pointer to the specified type at the memory address addr.
RawPointer
RawPointer[addr,type]
represents a raw pointer to the specified type at the memory address addr.
Details
- RawPointer is returned by functions such as ForeignFunction, RawMemoryAllocate, etc.
- Functions that allocate raw memory will return a memory-managed pointer.
- OpaqueRawPointer can be used to represent untyped pointers.
- RawPointer[ptr,type] can be used to cast a pointer to a typed pointer.
- Information for a RawPointer may include the following properties:
-
"Address" memory address "Type" referenced type - Properties of a RawPointer can be extracted with RawPointer[…][prop].
- Properties that can be extracted include all Information properties listed above.
Examples
open all close allBasic Examples (1)
Export a string as raw memory represented by a managed RawPointer:
str = "a string";
ptr = RawMemoryExport[str]rawPtr = UnmanageObject@ptrWrite a value to the raw memory:
RawMemoryWrite[rawPtr, 65];Import the raw memory as a string:
RawMemoryImport[rawPtr, {"String", StringLength[str]}]RawMemoryFree[rawPtr]Scope (1)
Information (1)
Create a pointer to raw memory:
rawPtr = UnmanageObject[RawMemoryExport["a string"]]Get Information about the pointer:
Information[rawPtr]Get the memory address of the pointer:
rawPtr["Address"]rawPtr["Type"]Properties & Relations (5)
Functions like RawMemoryAllocate return a memory-managed RawPointer:
ptr = RawMemoryAllocate["CInt", 10]Call UnmanageObject to take ownership of the managed pointer:
rawPtr = UnmanageObject[ptr]Notice that the memory management of the pointer allocated is no longer active:
ptr["Active"]Unmanaged pointers should be freed with RawMemoryFree to avoid memory leaks:
RawMemoryFree[rawPtr]NullRawPointerQ returns True if a raw pointer has 0 as its address:
NullRawPointerQ[RawPointer[0, "UnsignedInteger8"]]rawPtr = UnmanageObject@RawMemoryAllocate["CFloat"]Convert a RawPointer into an OpaqueRawPointer:
opaqueRawPtr = OpaqueRawPointer[rawPtr]Convert the opaque raw pointer back to a RawPointer:
RawPointer[opaqueRawPtr, "CFloat"]Allocate a raw memory for an object of type "UnsignedInteger8":
ptr = RawMemoryAllocate["UnsignedInteger8"]Write a value to the raw memory by passing the pointer to RawMemoryWrite:
RawMemoryWrite[ptr, 42]Read the raw memory with RawMemoryRead:
RawMemoryRead[ptr]Allocate a buffer of 10 "CLong" objects:
buffer = RawMemoryAllocate["CLong", 10]Use ForeignFunctionLoad to load a function that populates this buffer:
populateArray = ForeignFunctionLoad["compilerDemoBase", "populateArray", {"RawPointer"::["CLong"], "CLong"} -> "CInt"]Call the function by passing the buffer and its length:
populateArray[buffer, 10];Import the raw memory as a list with RawMemoryImport:
RawMemoryImport[buffer, {"List", 10}]Possible Issues (1)
RawPointer returns a Failure for invalid memory addresses:
RawPointer["0xAABBCC", "UnsignedInteger8"]Tech Notes
Related Guides
History
Text
Wolfram Research (2023), RawPointer, Wolfram Language function, https://reference.wolfram.com/language/ref/RawPointer.html.
CMS
Wolfram Language. 2023. "RawPointer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/RawPointer.html.
APA
Wolfram Language. (2023). RawPointer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/RawPointer.html
BibTeX
@misc{reference.wolfram_2026_rawpointer, author="Wolfram Research", title="{RawPointer}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/RawPointer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_rawpointer, organization={Wolfram Research}, title={RawPointer}, year={2023}, url={https://reference.wolfram.com/language/ref/RawPointer.html}, note=[Accessed: 12-June-2026]}