"CArray" (Compiled Type)

"CArray"::[type]

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

Details

  • "CArray"::[t] is equivalent to t* in C.
  • Objects with type "CArray" are not automatically memory managed, and must be manually freed.
  • "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)

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

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:

Possible Issues  (1)

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:

The function leaks memory:

Use DeleteObject to manually free the C arrays:

The resulting function does not leak memory: