creates a new pointer object in compiled code, suitable for use with external libraries.
creates a new object pointing to the initial value val.
stores val in the pointer p.
stores val in the "CArray" array at the given offset.
- ToRawPointer is only available in compiled code.
- ToRawPointer allocates memory on the stack.
- A pointer generated by ToRawPointer is invalidated when the function in which it was generated returns.
- FromRawPointer can be used to extract the value of a pointer generated by ToRawPointer.
- ToRawPointer[val] creates a copy of val and returns its address. Changes to that copy will not affect val, and vice versa.
- Usages of ToRawPointer have the following approximate equivalents in C:
p=ToRawPointer int x; int* p = &x; p=ToRawPointer[val] int x = val; int* p = &x; ToRawPointer[p,val] *p = val; ToRawPointer[array,offset,val] array[offset] = val; or *(array+offset) = val;
Examplesopen allclose all
Compile a program that uses ToRawPointer to populate a "CArray" so it can be passed to sumArray:
Possible Issues (2)
Because ToRawPointer[val] creates a pointer to a copy of val, functions that modify the pointer will not modify the original val.
Because ToRawPointer[n] creates a pointer to a copy of n, and incrementInput only modifies the value referenced by the pointer, n remains unchanged:
Pointers generated by ToRawPointer are invalidated when the function in which they were generated returns. Using a pointer after it has been invalidated can lead to crashes.
Compiling a function that calls FromRawPointer on an invalid pointer can lead to crashes:
Wolfram Research (2022), ToRawPointer, Wolfram Language function, https://reference.wolfram.com/language/ref/ToRawPointer.html.
Wolfram Language. 2022. "ToRawPointer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ToRawPointer.html.
Wolfram Language. (2022). ToRawPointer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ToRawPointer.html