WOLFRAM

"CArray"::[type]

represents an array type compatible with C, containing elements of the specified type.

Details

  • "CArray" can be used in Typed and related constructs to specify a type.
  • Objects with type "CArray" are not automatically memory managed and must be manually freed.
  • "CArray"::[t] is equivalent to t* in C.
  • "CArray" can be used when interfacing with external libraries that are compatible with C.
  • Internally, objects with type "CArray" are represented with pointers.

Constructors

Properties

  • Information[carr,"ElementType"] for carr of type "CArray"::[type] gives type .
  • FromRawPointer[carr,offset] indexes the C array carr. Indexes start with 0.

Conversions

Runtime Errors

  • Indexing is an unsafe operation, and indexing past the end of a C array can lead to crashes.

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Create a memory-managed C array, and then access one of its elements:

Out[1]=1
Out[2]=2

Represent a function from an external library that takes a C array as an argument:

Compile a program that creates a C array and calls the function:

Out[2]=2
Out[3]=3

Possible Issues  (1)Common pitfalls and unexpected behavior

C arrays created outside of a "Managed" object are not automatically memory managed, and must be freed manually. Compile a function that creates unmanaged C arrays:

Out[1]=1

The function leaks memory:

Out[2]=2

Use DeleteObject to manually free the C arrays:

Out[3]=3

The resulting function does not leak memory:

Out[4]=4