Removal of Unused Class Properties in Generated C/C++ Code
C/C++ code generated by Embedded Coder®contains unused class properties or structure fields. Removing unused properties or fields helps improve the memory footprint and run time of the code. By default, the code generator removes properties or fields that are unused in the generated C/C++ code.
This feature applies to standalone code generation. You can preserve the unused properties or fields in generated C/C++ code by using theMATLAB®Coder™app or at the command line.
Preserve Unused Class Properties or Structure Fields by usingMATLABCoder应用程序
To preserve unused properties or fields:
Open theMATLAB Coder应用程序。
On theGenerate Codepage, clickMore Settings.
On theMemorytab, select thePreserve unused fields and propertiescheck box.
Preserve Unused Class Properties or Structure Fields at the Command Line
To preserve the unused properties or fields, set the configuration parameterPreserveUnusedStructFields
totrue
and use the configuration objectcfg
in thecodegen
command.
cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true;
Examples
The following examples demonstrate the usage of thePreserveUnusedStructFields
configuration option to generate C/C++ code.
Generate C++ Code for MATLAB Classes
This table compares the generated C++ code formyClass
, with unused properties removed and with unused properties preserved.
MATLAB Code | Unused Class Properties Removed (default) |
Unused Class Properties Preserved |
---|---|---|
% Entry-point functionfunctiony = myAdd(n)%#codegeno = myClass(n); y = o.a + o.b;end classdefmyClasspropertiesa b cendmethodsfunctionobj = myClass(x) coder.inline('never'obj。= x;obj。b = x + 1;obj.c = x + 2;endendend |
Code generation command cfg = coder.config('lib'); cfg.TargetLang ="C++"; codegenmyAdd-args{5}-configcfg-report Generated C++ Code class myClass { public: void init(double x); double a; double b; }; void myClass::init(double x) { a = x; b = x + 1.0; } |
Code generation command cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; cfg.TargetLang ="C++"; codegenmyAdd-args{5}-configcfg-report Generated C++ Code class myClass { public: void init(double x); double a; double b; double c; }; void myClass::init(double x) { a = x; b = x + 1.0; c = x + 2.0; } |
Generate C Code for MATLAB Structures
This table compares the generated C code formyStruct
, with unused fields removed and with unused fields preserved.
MATLAB Code | Unused Structure Fields Removed (default) |
Unused Structure Fields Preserved |
---|---|---|
% Entry-point functionfunctionout = myStruct(n)%# codegens.a = [n n n]; s.b = n+2; s.c = n; out = myAdd([s s]);end functionout = myAdd(s) coder.inline('never'); out = s(1).b + s(2).b;end |
Code generation command cfg = coder.config('lib'); codegenmyStruct-args{0}-configcfg-report Generated C Code typedef struct { double b; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.b = n + 2.0; b_s[0] = s; b_s[1] = s; return myAdd(b_s); } |
Code generation command cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; codegenmyStruct-args{0}-configcfg-report Generated C Code typedef struct { double a[3]; double b; double c; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.a[0] = n; s.a[1] = n; s.a[2] = n; s.b = n + 2.0; s.c = n; b_s[0] = s; b_s[1] = s; return myAdd(b_s); } |
Usage Notes
When
PreserveUnusedStructFields
is set tofalse
, the code generator preserves properties or fields of the entry-point function input and output arguments and values passed tocoder.ceval
.When
PreserveUnusedStructFields
is set totrue
, the code generator removes properties or fields whose data types are unknown.