coder.nullcopy
Declare uninitialized variables in generated code
Syntax
Description
You can use coder.nullcopy
to optimize the generated code by
declaring uninitialized variables without copying variable values. Use
coder.nullcopy
only in two specific situations:
At code generation time, you see one of these errors:
For code generation, all variables must be fully defined before use. For code generation, all cell array elements must be fully defined before use.
However, you have fully defined all variables and cell array elements on all execution paths, and you have considered and rejected other troubleshooting solutions. See Resolve Issue: Variables Must Be Fully Defined Before Use (MATLAB Coder) and Resolve Issue: Cell Array Elements Must Be Fully Defined Before Use.
You see redundant variable assignments in the generated code. That is, a variable is assigned a value and then assigned a different value before it is used.
Use coder.nullcopy
with caution. In most cases, the code generator
automatically performs an optimization corresponding to coder.nullcopy
,
even if you do not include the coder.nullcopy
function in your
MATLAB® code. If you declare a variable using coder.nullcopy
and
do not fully assign the variable before you use it, the behavior of the generated code can
be unpredictable.
copies the type, size, and complexity of X
= coder.nullcopy(A
)A
to X
,
but does not copy element values. In the generated code,
coder.nullcopy
declares an uninitialized variable, preallocating
memory for X
without incurring the overhead of initializing memory.
In MATLAB execution, coder.nullcopy
returns the input
value.
In most cases, coder.nullcopy
applies recursively. That is, if
A
is an aggregate type (a class, structure, or cell array),
coder.nullcopy
also preallocates memory for the fields,
properties, or elements of A
. However, if A
is an
aggregate type that contains one or more variable-size arrays, special considerations
apply. See Declare Variable-Size Arrays Within Classes, Structures, or Cell Arrays.
Before you use or return X
, you must assign values to
all of its elements. Otherwise, the behavior of the
generated code can be unpredictable.
Examples
Input Arguments
Limitations
You cannot use
coder.nullcopy
on sparse matrices.You cannot use
coder.nullcopy
with classes that support overloaded parentheses or require indexing methods to access their data, such astable
.
Extended Capabilities
Version History
Introduced in R2011a