필터 지우기
필터 지우기

Creating array using emx array api with initialization to zero

조회 수: 6 (최근 30일)
Dmitri Bliznuk
Dmitri Bliznuk 2024년 2월 15일
댓글: Dmitri Bliznuk 2024년 3월 19일
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?

채택된 답변

Ezra Stein
Ezra Stein 2024년 3월 18일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by