Creating array using emx array api with initialization to zero

2 visualizaciones (últimos 30 días)
I have MATLAB Coder project which contains emx arrays.
When I generate C code in MATLAB R2021a, Coder generates emxCreate function with calloc inside:
emxArray_Nodes *emxCreateND_Nodes(int numDimensions, const int *size)
{
emxArray_Nodes *emx;
int numEl;
emxInit_Nodes(&emx, numDimensions);
numEl = 1;
for (int i = 0; i < numDimensions; i++) {
numEl *= size[i];
emx->size[i] = size[i];
}
emx->data = (Nodes *)calloc(static_cast<unsigned int>(numEl), sizeof(Nodes));
emx->numDimensions = numDimensions;
emx->allocatedSize = numEl;
return emx;
}
But when I generate C code in MATLAB R2023b, Coder generates emxCreate funciotn with malloc:
emxArray_Nodes *emxCreateND_Nodes(int numDimensions, const int *size)
{
emxArray_Nodes *emx;
int numEl;
emxInit_Nodes(&emx, numDimensions);
numEl = 1;
for (int i = 0; i < numDimensions; i++) {
numEl *= size[i];
emx->size[i] = size[i];
}
emx->data = static_cast<Nodes *>(
malloc(static_cast<unsigned int>(numEl) * sizeof(Nodes)));
emx->numDimensions = numDimensions;
emx->allocatedSize = numEl;
return emx;
}
In the second code memory for my struct arrays is allocated without initialization to zero. As a result, after updating MATLAB, code behaviour was changed.
How can I force MATLAB Coder R2023b to generate emx Create functions with calloc inside for initialization all struct array elements with zeros?

Respuesta aceptada

Ezra Stein
Ezra Stein el 18 de Mzo. de 2024
Hi Dmitri,
In R2023b we began generating emxArray allocation functions without zero-initializing their data. This was done as a performance enhancement because the generated code never actually relied on the zero-initialized data internally. Unfortunately, there is currently no way to enable the old behavior.
However, there are two possible workarounds:
  1. Manually initialize the data to 0 after creating the emxArray. This is the simplest approach but it incurs the preformance overhead of doing an allocation followed by a zero-initialization.
  2. Allocate memory via calloc manually, then wrap that memory in an emxArray using the emxCreateWrapper_<name> or emxCreateWrapperND_<name> utility functions. More details about this workflow can be found in the documentation here: https://www.mathworks.com/help/coder/ug/use-c-arrays-in-the-generated-function-interfaces.html#mw_1bf6258f-c070-4743-bb63-500d05e4542e
I am aware that the linked documentation page still states that the emxArray creation functions zero initialize the data. As a result, users have been relying on this behavior with an expectation that it will not change without warning. I am sorry for the inconvenience that this breaking change has caused.
I will discuss with our team to consider an enhancement that would allow users to enable the old behavior if desired.
Best,
-Ezra

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by