Coder: Does not allocate output structure
3 views (last 30 days)
Show older comments
I'm generating code for a DLL. It has an initialization call that returns a configuration output structure (Cfg), which gets fed into other calls. The code that gets generated does not actually allocate this output structure, and also does not generate an emxCreateCfg nor an emxDestroyCfg. Below is simplified example code which demonstrates this problem. (I'm using 2104a.)
function [cfg, error] = Init(fileName) %#codegen
error = '';
cfg = structCfg();
% opens the file, and sets everything in cfg
end
function s = structCfg() %#codegen
s.Version = '?';
s.A = pi/4;
s.B = 5.0;
% simplified example, actually has many more elements
coder.cstructname(s,'Cfg');
coder.varsize('s.Version', [1, inf], [0, 1]);
end
Code Generated for Init.c: (also, where's my empty error output?!)
void Init(const emxArray_char_T *fileName, Cfg *cfg)
{
(void)fileName;
cfg->Version.size[0] = 1;
cfg->Version.size[1] = 1;
cfg->Version.data[0] = '?';
cfg->A = 0.78539816339744828;
cfg->B = 5.0;
}
Code from Init_emxAPI.h, no helper fuctions for struct Cfg
extern emxArray_char_T *emxCreateND_char_T(int numDimensions, int *size);
extern emxArray_char_T *emxCreateWrapperND_char_T(char *data, int
numDimensions, int *size);
extern emxArray_char_T *emxCreateWrapper_char_T(char *data, int rows, int cols);
extern emxArray_char_T *emxCreate_char_T(int rows, int cols);
extern void emxDestroyArray_char_T(emxArray_char_T *emxArray);
0 Comments
Answers (2)
Alexander Bottema
on 19 Sep 2014
Hi Roger,
The parts you wish to interact with for the generated code must be related to the entry points of the compiled function. If you have multiple functions you would like to call (from the outside), then all of these have to be specified as entry points. For example,
codegen foo1.m -args { example args for foo1 } foo2.m -args { example args for foo2 } ...
All other functions that get generated by MATLAB Coder are not part of the API can be optimized which includes inlining, unused inputs/outputs optimizations, multiple specializations, etc.
Alexander
0 Comments
John Elliott
on 19 Sep 2014
Hi Roger, When calling entry-point functions that return emxArray variables (or structures containing emxArray fields), you must initialize the emxArrays to their empty state before calling the entry-point function. This gives you flexibility regarding ownership of the emxArray memory, even though it might at first seem a little unusual to have to initialize function outputs before calling the function.
One thing to keep in mind is that you must initialize all the emxArrays in the output variable. For example, if the output is a structure containing fields that are emxArrays, then you must initialize each of these fields. Similarly, if you have nested structures or arrays, you must recursively initialize each level of the nesting.
Finally, to initialize an exmArray to its empty state means that the size vector must be properly initialized, i.e. the static dimensions (if any) need have the right values, and the dynamic dimensions should be zero.
Hope this helps. -John
See Also
Categories
Find more on Automated Fixed-Point Conversion in MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!