ToRawPointer

ToRawPointer[]

creates a new pointer object in compiled code, suitable for use with external libraries.

ToRawPointer[val]

creates a new object pointing to the initial value val.

ToRawPointer[p,val]

stores val in the pointer p.

ToRawPointer[array,offset,val]

stores val in the "CArray" array at the given offset.

Details

  • 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.
  • ToRawPointer[array,offset,val] can store elements in an array.
  • 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;

Examples

open allclose all

Basic Examples  (1)

Create a pointer to an integer, then dereference it:

Scope  (2)

Update  (1)

ToRawPointer can update an existing pointer:

Offset  (1)

ToRawPointer can assign different elements of an array:

Applications  (2)

Represent a library function that returns its results by modifying a pointer:

Compile a program that uses the function:

Represent a library function that sums a "CArray":

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.

Represent a function that modifies a pointer:

Compile a program that initializes the pointer with a variable, calls the function that modifies the pointer and then returns the original variable:

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.

Define a function that returns an invalid pointer:

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.

Text

Wolfram Research (2022), ToRawPointer, Wolfram Language function, https://reference.wolfram.com/language/ref/ToRawPointer.html.

CMS

Wolfram Language. 2022. "ToRawPointer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ToRawPointer.html.

APA

Wolfram Language. (2022). ToRawPointer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ToRawPointer.html

BibTeX

@misc{reference.wolfram_2024_torawpointer, author="Wolfram Research", title="{ToRawPointer}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/ToRawPointer.html}", note=[Accessed: 15-October-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_torawpointer, organization={Wolfram Research}, title={ToRawPointer}, year={2022}, url={https://reference.wolfram.com/language/ref/ToRawPointer.html}, note=[Accessed: 15-October-2024 ]}