"RawPointer" (Compiled Type)
-
See Also
- ToRawPointer
- FromRawPointer
-
- Compiled Types
- CArray
- OpaqueRawPointer
- Related Guides
- Tech Notes
"RawPointer" (Compiled Type)
"RawPointer"::[t]
represents a pointer to an object with type t, suitable for use with external libraries.
Constructors
- ToRawPointer can be used to create a pointer object that uses memory allocated on the stack and is valid until the enclosing function returns.
- Unless wrapped with "Managed", pointer objects constructed with CreateTypeInstance are not automatically memory managed and must be manually freed with DeleteObject.
- CreateTypeInstance["RawPointer"::[type]] constructs a raw pointer.
- CreateTypeInstance["Managed"::["RawPointer"::[type]]] constructs a memory-manged C raw pointer.
Properties
- Information[ptr,"BaseType"] for ptr of type "RawPointer"::[t] gives "TypeSpecifier"::[t].
- FromRawPointer[ptr] returns the value pointed to by ptr.
Conversions
- RawPointer expressions can be converted into raw pointer types in compiled code.
- "RawPointer" compiled elements can be converted into RawPointer expressions. Note that the raw pointer in compiled code must be allocated dynamically with CreateTypeInstance.
- Cast can be used to bitcast pointers to and from C arrays.
- Cast can be used to bitcast pointers to and from opaque pointers.
From Expressions
To Expressions
"CArray"
"OpaqueRawPointer"
Run-Time Errors
- Dereferencing a pointer with FromRawPointer is an unsafe operation. Attempting to dereference an invalid pointer can lead to a crash.
Examples
Basic Examples (4)
Create a "RawPointer"::["MachineInteger"], then dereference it:
cf = FunctionCompile[Function[Typed[arg, "Integer64"],
With[{p = ToRawPointer[arg]}, FromRawPointer[p]]
]]cf[4]Represent a library function that returns its results by modifying a pointer:
funcDec = LibraryFunctionDeclaration["addonePointer", "compilerDemoBase", {"CInt", "RawPointer"::["CInt"]} -> "CInt"];Compile a program that uses the function:
cf = FunctionCompile[funcDec,
Function[Typed[n, "CInt"], Module[{ptr},
ptr = Typed[ToRawPointer[], "RawPointer"::["CInt"]];
LibraryFunction["addonePointer"][n, ptr];
FromRawPointer[ptr]
]]]cf[12]Compile a program that returns a raw pointer containing a value:
cf = FunctionCompile[
Function[Typed[n, "Integer64"], Module[{ptr},
ptr = CreateTypeInstance["RawPointer"];
ToRawPointer[ptr, n];
ptr
]]]rp = cf[12]A compiled function that accepts the raw pointer, extracts the value and deletes the pointer:
cf1 = FunctionCompile[
Function[Typed[rp, "RawPointer"::["Integer64"]], Module[{val}, val = FromRawPointer[rp];DeleteObject[rp];val]
]]cf1[rp]Compile a program that returns a managed raw pointer containing a value:
cf = FunctionCompile[
Function[Typed[n, "Integer64"], Module[{ptr},
ptr = CreateTypeInstance["Managed"::["RawPointer"::["Integer64"]]];
ToRawPointer[ptr, n];
ptr
]]]rp = cf[12]A compiled function that accepts the managed raw pointer and extracts the value:
cf1 = FunctionCompile[
Function[Typed[rp, "Managed"::["RawPointer"::["Integer64"]]], FromRawPointer[rp]
]]The pointer should not be manually freed, since that is done automatically by "Managed":
cf1[rp]Tech Notes
Related Guides
History
Introduced in 2022 (13.1)