Main Content

Best Practices for Defining Variables for C/C++ Code Generation

在使用之前,通过分配定义变量

For C/C++ code generation, you should explicitly and unambiguously define the class, size, and complexity of variables before using them in operations or returning them as outputs. Define variables by assignment, but note that the assignment copies not only the value, but also the size, class, and complexity represented by that value to the new variable. For example:

一种ss一世gnment: Defines:
a = 14.7; 一种作为真正的双重标量。
b = a; b具有属性一种(real double scalar).
C =零(5,2); C作为真正的5 x-2双打。
d = [1 2 3 4 5;6 7 8 9 0]; d作为真正的5 x-2双打。
y = int16(3); y一种s一种real 16-bit integer scalar.

以这种方式定义属性,以便在C/C ++代码生成期间所需的执行路径上定义变量。

您分配给变量的数据可以是标量,矩阵或结构。如果您的变量是一个结构,请明确定义每个字段的属性。

Initializing the new variable to the value of the assigned data sometimes results in redundant copies in the generated code. To avoid redundant copies, you can define variables without initializing their values by using theCoder.nullcopy如描述消除生成代码中变量的冗余副本

When you define variables, they are local by default; they do not persist between function calls. To make variables persistent, see执着的

示例1.为多个执行路径定义变量

考虑以下MATLAB®代码:

...如果C> 0X= 11; end % Later in your code ... if c > 0 use(x); end ...
这里,X一世s一种ss一世gned only ifC> 0并且仅在C> 0。This code works in MATLAB, but generates a compilation error during code generation because it detects thatX一世sundefined on some execution paths (whenC <= 0)。

To make this code suitable for code generation, defineX在使用之前:

X= 0;...如果C> 0X= 11; end % Later in your code ... if c > 0 use(x); end ...

示例2.在结构中定义字段

考虑以下MATLAB代码:

...如果C> 0 s.a = 11;disp(s);else s.a = 12;s.b = 12;end % Try to use s use(s); ...
在这里,第一部分如果statement uses only the field一种,和别的Clause uses fields一种一种ndb。该代码在MATLAB中起作用,但在C/C ++代码生成过程中生成了汇编错误,因为它检测到结构类型不匹配。为了防止此错误,请在结构上执行某些操作后,请勿将字段添加到结构中。有关更多信息,请参阅代码生成的结构定义

To make this code suitable for C/C++ code generation, define all fields ofs使用之前。

...% Define all fields in structure ss = struct('a',0,'b',0);如果C> 0 s.a = 11;disp(s);else s.a = 12;s.b = 12;end%使用s使用;...

Use Caution When Reassigning Variables

通常,您应该遵守C/C ++代码生成的“一种变量/一种类型”规则;也就是说,每个变量必须具有特定的类,大小和复杂性。通常,如果您在初始分配后重新分配变量属性,则在代码生成期间会遇到汇编错误,但是有例外,如图所述重新分配可变属性

在可变定义中使用类型的铸造运算符

默认情况下,常数为类型双倍的。To define variables of other types, you can use type cast operators in variable definitions. For example, the following code defines variabley作为整数:

... x = 15;默认情况下,%x是双重型。y = uint8(x);%y具有x的值,但铸造为uint8。...

在分配索引变量之前定义矩阵

从MATLAB生成C/C ++代码时,您不能通过将变量写入超出其当前大小的元素来生长。这样的索引操作会产生运行时错误。在将值分配给其元素之前,您必须先定义矩阵。

例如,代码生成不允许以下初始分配:

g(3,2) = 14.6; % Not allowed for creating g % OK for assigning value once created

有关索引矩阵的更多信息,请参见代码生成的矩阵索引操作中与MATLAB不兼容

索引数组通过使用常数值向量

最好使用常数值向量来索引数组,而不是使用包含非恒定对象的范围。

In some cases, the code generator is unable to determine whether an expression containing the冒号operator is fixed-size or variable-size. Use constant value vectors to index arrays to prevent them from being unnecessarily created as variable-size arrays in the generated code.

For example, the arrayout已通过使用变量创建一世一世ndexed through the random row vector一种

...%提取元素i至i+5用于处理a = rand(1,10);out = a(i:i+5);%如果我在编译时间未知,则是可变大小...

如果一世是一个编译时代常数值,代码生成器生成一个固定尺寸的对象out。如果一世在编译时间未知,代码生成器会为out在生成的代码中。

为了防止代码生成器创建可变大小数组,以此模式重写了先前的代码段:

...%提取元素i至i+5用于处理a = rand(1,10);out = A (i+(0:5)); % out is fixed-size, even if i is unknown at compile time ...

This pattern enables you to generate fixed-size arrays that have iterator values unknown at compile-time. Another example of the recommended rewrite is:

宽度= 25;a = a(j宽:j+width);%a是可变大小的,如果j在编译时间fsa = a(j+( - 宽度:width)));%这是固定尺寸的,即使J在编译时间未知...

也可以看看

|

Related Topics