Representation of Arrays in Generated Code
The code generator produces C/C++ array definitions that depend on the array element type and whether the array uses static or dynamic memory allocation. Use the generated array implementations to interface your arrays with the generated code.
Memory allocation for arrays require different implementations:
For a fixed-size array or a variable-size array whose size is bounded within a predefined memory threshold, the generated C/C++ definition consists of a fixed-size array of elements and a size vector that stores the total number of array elements. In some cases, the fixed-size element array and the size vector are stored within a structure. The memory for this array comes from the program stack and is statically allocated.
For an array whose size is unbounded at compile time, or whose bounds exceed the predefined threshold, the generated C definition consists of a data structure called an
emxArray
. The generated C++ definition consists of acoder::array
class template.
The predefined threshold size (in bytes) is specified in your configuration objects.
The default value of the parameter is 65536
. See
DynamicMemoryAllocationThreshold
in coder.MexCodeConfig
, coder.CodeConfig
, or coder.EmbeddedCodeConfig
.
For dynamically allocated arrays, the run-time allocated size is set based on the current array size. During program execution, as run-time allocated size is exceeded, the generated code reallocates additional memory space from the heap and adds it to the dynamic array storage.
This table lists a few typical cases for array representation in the generated code.
Algorithm Description and Array Size | MATLAB® Function | Generated C/C++ Code |
---|---|---|
Create a fixed-size 1-by-500 row vector. The array is the output of the MATLAB function The generated code allocates memory to a fixed-size vector on the program stack. |
function B = create_vec0 %#codegen B = zeros(1,500); end | void create_vec0(double B[500]) { memset(&B[0], 0, 500U * sizeof(double)); } The array is the input to the function in the generated code. |
Create a fixed-size 1-by-20 row vector. Declare the array as variable-size with bounds at 500 elements. Assign this variable-size array to the input array. This array is bound within the size threshold and is the input to the function in the generated code. |
function create_vec1(B) %#codegen A = zeros(1,20); coder.varsize('A',[1 500],[0 1]); B = A; end |
void create_vec1(double B_data[], int B_size[2]) { int i; B_size[0] = 1; B_size[1] = 20; for (i = 0; i < 20; i++) { B_data[i] = 1.0; } } Note The generated code includes the inputs in the function parameters. |
Create a local fixed-size 1-by-20000 row vector. Declare the array as variable-size with bounds at 30,000 elements. The variable-size array exceeds the predefined dynamic memory allocation threshold. This array is stored on heap memory. The generated code includes the output array in the function parameter. |
function B = create_vec2() %#codegen A = ones(1,20000); coder.varsize("A",[1 30000], [0 1]); B = [1 A]; end | C: void create_vec2(emxArray_real_T *B) { double *B_data; int i; i = B->size[0] * B->size[1]; B->size[0] = 1; B->size[1] = 20001; emxEnsureCapacity_real_T(B, i); B_data = B->data; B_data[0] = 1.0; for (i = 0; i < 20000; i++) { B_data[i + 1] = 1.0; } } C++: void create_vec2(coder::array<double, 2U> &B) { B.set_size(1, 20001); B[0] = 1.0; for (int i{0}; i < 20000; i++) { B[i + 1] = 1.0; } } |
Create an array that has the size determined by an unbounded integer input. The generated array size is unknown and unbounded at compile time. |
function y = create_vec3(n) %#codegen y = ones(1,n,'int8'); | C: void create_vec3(double n, emxArray_int8_T *y) { int i; int loop_ub_tmp; signed char *y_data; i = y->size[0] * y->size[1]; y->size[0] = 1; loop_ub_tmp = (int)n; y->size[1] = (int)n; emxEnsureCapacity_int8_T(y, i); y_data = y->data; for (i = 0; i < loop_ub_tmp; i++) { y_data[i] = 1; } } C++: void create_vec3(double n, coder::array<signed char, 2U> &y) { int loop_ub_tmp; loop_ub_tmp = static_cast<int>(n); y.set_size(1, loop_ub_tmp); for (int i{0}; i < loop_ub_tmp; i++) { y[i] = 1; } } |
To learn about the emxArray
data structure, see Use C Arrays in the Generated Function Interfaces.
To learn about the coder::array
class template, see Use Dynamically Allocated C++ Arrays in Generated Function Interfaces.
Customize Interface Generation
By default, the generated C++ code uses the coder::array
template to implement dynamically allocated arrays. You can choose to generate C++
code that uses the C style emxArray
data structure to implement
dynamically allocated arrays. To generate C style emxArray
data
structures, do either of the following:
In a code configuration object (
coder.MexCodeConfig
,coder.CodeConfig
, orcoder.EmbeddedCodeConfig
), set theDynamicMemoryAllocationInterface
parameter to'C'
.Alternatively, In the MATLAB Coder™ app, on the Memory tab, set Dynamic memory allocation interface to
Use C style EmxArray
.
To create dynamically allocated arrays for variable-size arrays in the generated code, do either of the following:
Set the
EnableDynamicMemoryAllocation
flag totrue
.Alternatively, in the MATLAB Coder App, on the Memory tab, select the Enable dynamic memory allocation option.
By default, arrays that are bounded within a threshold size do not use dynamic allocation in the generated code. Alternatively, you can disable dynamic memory allocation and change the dynamic memory allocation threshold. See Control Memory Allocation for Variable-Size Arrays.
See Also
coder.config
| coder.MexCodeConfig
| coder.CodeConfig
| coder.EmbeddedCodeConfig