Main Content

Removal of Unused Class Properties in the 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.

To preserve the unused properties or fields, set the configuration parameter PreserveUnusedStructFields to true.

The PreserveUnusedStructFields configuration option applies to standalone code generation.

Preserve Unused Class Properties by using MATLAB Coder App

To preserve unused properties or fields:

  1. Open the MATLAB® Coder™ App.

  2. On the Generate Code page, click More Settings.

  3. On the Memory tab, select the Preserve unused fields and properties check box.

MATLAB Coder GUI for Preserve unused fields and properties

Preserve Unused Class Properties at the Command Line

You can preserve the unused properties or fields at the command-line interface:

cfg = coder.config('lib');
cfg.PreserveUnusedStructFields = true;

For example, consider the MATLAB class myClass and the entry point function myAdd.

classdef myClass
    properties    
        a        
        b
        c    
    end

    methods
        function obj = myClass(x)
            coder.inline('never')    
            obj.a = x;
            obj.b = x + 1;
            obj.c = x + 2;
        end
    end
end
function y = myAdd(n)
    %#codegen
    o = myClass(n);
    y = o.a + o.b; % class property ‘c’ is unused
end

At the MATLAB command line, run this codegen command:

codegen myAdd -args {5} -config cfg -lang:C++ -report

Open the code generation report to see the generated code. The unused property c is preserved in the generated code.

class myClass {

    public:
        void init(double x);
        double a;    
        double b;
        double c;
};

Usage Notes and Limitations

  • When PreserveUnusedStructFields is set to false, the code generator preserves properties or fields of the entry-point function input and output arguments and values passed to coder.ceval.

  • When PreserveUnusedStructFields is set to true, the code generator removes properties or fields whose data types are unknown.

  • The class properties or structure fields are preserved when properties or fields are assigned but not used.

See Also

|