When using MATLAB Coder to generate C code and there is a dynamic array, the foo_emxutil.c that is created is allocating memory as a power of 2?
4 views (last 30 days)
Show older comments
Using MATLAB Coder, the C code was generated for the following function.
function y = mainFunc(X,q)
x = single(X(1:2:q,1)) + 1i*single(X(2:2:q,1));
y = (x.')*x;
end
This function has a dynamic array x an in the process of generating the C code, the following main_emxutil.c is generated.
/*
* File: mainFunc_emxutil.c
*
* MATLAB Coder version : 5.3
* C/C++ source code generated on : 19-Dec-2023 16:20:39
*/
/* Include Files */
#include "mainFunc_emxutil.h"
#include "mainFunc_types.h"
#include <stdlib.h>
#include <string.h>
/* Function Definitions */
/*
* Arguments : emxArray_creal32_T_main_main *emxArray
* int oldNumel
* Return Type : void
*/
void emxEnsureCapacity_creal32_T_main(emxArray_creal32_T_main_main *emxArray,
int oldNumel)
{
int i;
int newNumel;
void *newData;
if (oldNumel < 0) {
oldNumel = 0;
}
newNumel = 1;
for (i = 0; i < emxArray->numDimensions; i++) {
newNumel *= emxArray->size[i];
}
if (newNumel > emxArray->allocatedSize) {
i = emxArray->allocatedSize;
if (i < 16) {
i = 16;
}
while (i < newNumel) {
if (i > 1073741823) {
i = MAX_int32_T;
} else {
i *= 2;
}
}
newData = calloc((unsigned int)i, sizeof(creal32_T));
if (emxArray->data != NULL) {
memcpy(newData, emxArray->data, sizeof(creal32_T) * oldNumel);
if (emxArray->canFreeData) {
free(emxArray->data);
}
}
emxArray->data = (creal32_T *)newData;
emxArray->allocatedSize = i;
emxArray->canFreeData = true;
}
}
/*
* Arguments : emxArray_creal32_T_main_main **pEmxArray
* Return Type : void
*/
void emxFree_creal32_T_main(emxArray_creal32_T_main_main **pEmxArray)
{
if (*pEmxArray != (emxArray_creal32_T_main_main *)NULL) {
if (((*pEmxArray)->data != (creal32_T *)NULL) &&
(*pEmxArray)->canFreeData) {
free((*pEmxArray)->data);
}
free((*pEmxArray)->size);
free(*pEmxArray);
*pEmxArray = (emxArray_creal32_T_main_main *)NULL;
}
}
/*
* Arguments : emxArray_creal32_T_main_main **pEmxArray
* Return Type : void
*/
void emxInit_creal32_T_main(emxArray_creal32_T_main_main **pEmxArray)
{
emxArray_creal32_T_main_main *emxArray;
*pEmxArray = (emxArray_creal32_T_main_main *)malloc(
sizeof(emxArray_creal32_T_main_main));
emxArray = *pEmxArray;
emxArray->data = (creal32_T *)NULL;
emxArray->numDimensions = 1;
emxArray->size = (int *)malloc(sizeof(int));
emxArray->allocatedSize = 0;
emxArray->canFreeData = true;
emxArray->size[0] = 0;
}
/*
* File trailer for mainFunc_emxutil.c
*
* [EOF]
*/
In the while loop, the allocated array size is increasing by a power of 2 (i *= 2). Is there way to avoid this such that the allocated size only increased by 1 (i++). I would like to avoid editing the generate C files. So if there is a MATLAB Coder setting or some other setting that I am missing that would help.
The following script was used to generate the C code.
%% Create configuration object of class 'coder.EmbeddedCodeConfig'.
cfg = coder.config('lib','ecoder',true);
cfg.CustomSymbolStrGlobalVar = '$M$N_main';
cfg.CustomSymbolStrType = '$M$N_main';
cfg.CustomSymbolStrEMXArray = 'emxArray_$M$N_main';
cfg.CustomSymbolStrEMXArrayFcn = 'emx$M$N_main';
cfg.FilePartitionMethod = 'SingleFile';
cfg.GenCodeOnly = true;
cfg.GenerateReport = true;
cfg.MATLABSourceComments = true;
cfg.MaxIdLength = 256;
cfg.PreserveVariableNames = 'UserNames';
cfg.ReportPotentialDifferences = false;
%% Define argument types for entry-point 'mainFunc'.
ARGS = cell(1,1);
ARGS{1} = cell(2,1);
ARGS{1}{1} = coder.typeof(int16(0),[40000000 1]);
ARGS{1}{2} = coder.typeof(int32(0));
%% Invoke MATLAB Coder.
codegen -config cfg mainFunc -args ARGS{1}
0 Comments
Accepted Answer
Shubh
on 26 Dec 2023
Hi,
I understand that you would like to know if there's any setting to increase the allocated array size by 1.
MATLAB Coder generates C/C++ code from MATLAB code, and it uses certain strategies to manage dynamic memory allocation efficiently. The doubling strategy (i *= 2) used in the generated code for memory allocation is a common approach in many programming scenarios, as it balances between frequent memory reallocations (which are costly in terms of performance) and excessive memory usage.
The specific behavior of MATLAB Coder's memory allocation strategy, including the doubling strategy, is not directly configurable through MATLAB Coder settings. This strategy is chosen because it provides a good compromise between speed and memory efficiency for a wide range of applications.
Hope this helps!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!