"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:
Represent a library function that returns its results by modifying a pointer:
Compile a program that uses the function:
Compile a program that returns a raw pointer containing a value:
A compiled function that accepts the raw pointer, extracts the value and deletes the pointer:
Compile a program that returns a managed raw pointer containing a value:
A compiled function that accepts the managed raw pointer and extracts the value:
The pointer should not be manually freed, since that is done automatically by "Managed":
Related Guides
Related Links
History
Introduced in 2022 (13.1)