"CArray" (Compiled Type)
"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
- Unless wrapped with "Managed", C arrays constructed with CreateTypeInstance are not automatically memory managed, and must be manually freed with DeleteObject. »
- CreateTypeInstance["CArray"::[type],len] constructs a C array with length len.
- CreateTypeInstance["CArray"::[type],carr,len] constructs a copy of the C array carr with length len.
- CreateTypeInstance["CArray"::[type],array] constructs a C array containing elements of the given array. Supported array types include "ListVector", "NumericArray" and "PackedArray".
- CreateTypeInstance["Managed"::["CArray"::[type]],…] constructs a memory-managed C array.
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
- Array types can be Cast to managed C arrays. Supported array types include "ListVector", "NumericArray" and "PackedArray".
- Cast can be used to bitcast C arrays to and from raw pointers.
Arrays
"RawPointer" and "OpaqueRawPointer"
Examples
open all close allBasic Examples (3)
Create a memory-managed C array, and then access one of its elements:
cf = FunctionCompile[Function[{}, Module[{carr},
carr = CreateTypeInstance["Managed"::["CArray"::["MachineInteger"]], {3, 1, 4}];
FromRawPointer[carr, 2]
]]]cf[]Represent a function from an external library that takes a C array as an argument:
dec = LibraryFunctionDeclaration["sumArray", "compilerDemoBase", {"CArray"::["CLong"], "CLong"} -> "CInt"];Compile a program that creates a C array and calls the function:
cf = FunctionCompile[dec,
Function[Typed[arg, "ListVector"::["CLong"]],
Module[{carr},
carr = CreateTypeInstance["Managed"::["CArray"::["CLong"]], arg];
LibraryFunction["sumArray"][carr, Length[arg]]
]
]]cf[Range[100]]Compile a program that creates a C array and then frees it:
cf = FunctionCompile[
Function[Typed[arg, "MachineInteger"],
Module[{carr},
carr = CreateTypeInstance[ "CArray"::["Real64"], arg];
DeleteObject[carr]
]
]]cf[10]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:
cf1 = FunctionCompile[Function[{},
With[{carr = CreateTypeInstance["CArray"::["Integer8"], 100]},
Null
]]]With[{before = MemoryInUse[]}, Do[cf1[], 10];MemoryInUse[] - before]Use DeleteObject to manually free the C arrays:
cf2 = FunctionCompile[Function[{},
With[{carr = CreateTypeInstance["CArray"::["Integer8"], 100]},
DeleteObject[carr]
]]]The resulting function does not leak memory:
With[{before = MemoryInUse[]}, Do[cf2[], 10];MemoryInUse[] - before]See Also
LibraryFunctionDeclaration FromRawPointer DeleteObject
Compiled Types: Managed CString RawPointer OpaqueRawPointer
Tech Notes
Related Guides
History
Introduced in 2022 (13.1)