主要内容

在生成的代码中按引用或值传递结构参数

此示例演示如何控制生成的入口点函数的结构参数是按引用传递还是按值传递。

通过引用传递使用指针来访问结构参数。如果函数写入输入结构的一个元素,则会覆盖输入值。传递值将生成输入或输出结构参数的副本。为了减少内存使用和执行时间,可以使用引用传递。

如果结构参数既是输入又是输出,则生成的入口点函数通过引用传递参数。生成的MEX函数通过引用传递结构参数。对于MEX函数输出,不能指定要按值传递结构参数。

使用MATLAB®Coder应用程序通过引用或值指定传递

打开生成对话框,对生成代码页面,点击生成箭头。

设置构建类型致下列任何一项:

  • 源代码

  • 静态库

  • 动态库

  • 可执行的

点击更多的设置

所有设置选项卡,设置通过引用入口点函数来传递结构选项:

  • 是,对于通过引用传递(默认)

  • 不,是传递值

使用命令行界面指定引用或值传递

为静态库、动态库或可执行程序创建代码配置对象。例如,为静态库创建一个代码配置对象。

cfg = coder.config(“自由”);

设置PassStructByReference属性:

  • 真正的,用于引用传递(默认)

  • ,用于按值传递

例如:

cfg。PassStructByReference = true;

通过引用传递输入结构参数

编写MATLAB函数my_struct_in它有一个输入结构参数。

类型my_struct_in.m
函数y = my_struct_in(s) %#codegen y = s.f;

定义一个结构变量mystruct在MATLAB®工作空间。

struct mystruct = (“f”1:4);

为C静态库创建一个代码生成配置对象。

cfg = coder.config (“自由”);

指定您希望通过引用传递结构参数。

cfg。PassStructByReference = true;

生成的代码。指定输入参数具有变量的类型mystruct

codegen配置cfgarg游戏{mystruct}my_struct_in
代码生成成功。

查看生成的C代码。

类型codegen / lib / my_struct_in / my_struct_in.c
/* *文件:my_struct_in. C * * MATLAB Coder版本:5.4 * C/ c++源代码生成于:26-Feb-2022 10:50:28 */ /*包含文件*/ # Include "my_struct_in.h" # Include "my_struct_in_types.h" /*函数定义*/ /* * *参数:const struct0_T *s * double y[4] *返回类型:void */ void my_struct_in(const struct0_T *s, double y[4]) {y[0] = s->f[0];y [1] = f s - > [1];y [2] = f s - > [2];y [3] = f s - > [3];} /* * my_struct_in.c * * [EOF] */

的生成函数签名my_struct_in

void my_struct_in(struct0_T *s, double y[4])

my_struct_in传递输入结构年代通过引用传递。

按值传递输入结构参数

指定要按值传递结构参数。

cfg。PassStructByReference = false;

生成的代码。指定输入参数具有变量的类型mystruct

codegen配置cfgarg游戏{mystruct}my_struct_in
代码生成成功。

查看生成的C代码。

类型codegen / lib / my_struct_in / my_struct_in.c
/* *文件:my_struct_in. C * * MATLAB Coder版本:5.4 * C/ c++源代码生成于:26-Feb-2022 10:50:31 */ /*包含文件*/ # Include "my_struct_in.h" # Include "my_struct_in_types.h" /*函数定义*/ /* * *参数:const struct0_T s * double y[4] *返回类型:void */ void my_struct_in(const struct0_T s, double y[4]) {y[0] = s.f[0];y[1] =顺丰速递[1];y[2] =顺丰速递[2];y[3] =顺丰速递[3];} /* * my_struct_in.c * * [EOF] */

的生成函数签名my_struct_in

构造函数my_struct_in(const struct0_T s, double y[4] .

my_struct_in传递输入结构年代的价值。

通过引用传递输出结构参数

编写MATLAB函数my_struct_out它有一个输出结构参数。

类型my_struct_out.m
函数s = my_struct_out(x) %#codegen s.f = x;

定义一个变量一个在MATLAB®工作空间。

一个= 1:4;

为C静态库创建一个代码生成配置对象。

cfg = coder.config (“自由”);

指定您希望通过引用传递结构参数。

cfg。PassStructByReference = true;

生成的代码。指定输入参数具有变量的类型一个

codegen配置cfgarg游戏{一}my_struct_out
代码生成成功。

查看生成的C代码。

类型codegen / lib / my_struct_out / my_struct_out.c
/* *文件:my_struct_out. C * * MATLAB Coder版本:5.4 * C/ c++源代码生成于:26-Feb-2022 10:50:34 */ /*包含文件*/ # Include "my_struct_out.h" # Include "my_struct_out_types.h" /*函数定义*/ /* *参数:const double x[4] * struct0_T *s *返回类型:void */ void my_struct_out(const double x[4], struct0_T *s) {s->f[0] = x[0];s - > f [1] = x [1];s - > f [2] = x [2];s - > f [3] = x [3];} /* * my_struct_out.c * * [EOF] */

的生成函数签名my_struct_out

struct0_struct_out (struct0_T *s)

my_struct_out传递输出结构年代通过引用传递。

按值传递输出结构参数

指定要按值传递结构参数。

cfg。PassStructByReference = false;

生成的代码。指定输入参数具有变量的类型一个

codegen配置cfgarg游戏{一}my_struct_out
代码生成成功。

查看生成的C代码。

类型codegen / lib / my_struct_out / my_struct_out.c
/* *文件:my_struct_out. C * * MATLAB Coder版本:5.4 * C/ c++源代码生成于:26-Feb-2022 10:50:37 */ /*包含文件*/ #包括"my_struct_out.h" #包括"my_struct_out_types.h" /*函数定义*/ /* * *参数:const double x[4] *返回类型:struct0_T */ struct0_T my_struct_out(const double x[4]) {struct0_T s;顺丰速递[0]= x [0];顺丰速递[1]= x [1];顺丰速递[2]= x [2];顺丰速递[3]= x [3];返回年代;} /* * my_struct_out.c * * [EOF] */

的生成函数签名my_struct_out

struct0_T my_struct_out(const double x[4])

my_struct_out返回一个输出结构。

通过引用传递输入和输出结构参数

当实参既是输入又是输出时,生成的C函数通过引用传递实参,即使是当PassStructByReference是假的。

编写MATLAB函数my_struct_inout它有一个结构参数既是输入参数又是输出参数。

类型my_struct_inout.m
函数[y,s] = my_struct_inout(x,s) %#codegen y = x + sum(s.f);

定义的变量一个和结构变量mystruct在MATLAB®工作空间。

一个= 1:4;struct mystruct = (“f”,);

为C静态库创建一个代码生成配置对象。

cfg = coder.config (“自由”);

指定要按值传递结构参数。

cfg。PassStructByReference = false;

生成的代码。指定第一个输入的类型为一个第二个输入的类型是mystruct

codegen配置cfgarg游戏{, mystruct}my_struct_inout
代码生成成功。

查看生成的C代码。

类型codegen / lib / my_struct_inout / my_struct_inout.c
/* *文件:my_struct_inout. C * * MATLAB Coder版本:5.4 * C/ c++源代码生成于:26-Feb-2022 10:50:40 */ /*包含文件*/ # Include "my_struct_inout.h" # Include "my_struct_inout_types.h" /*函数定义*/ /* * *参数:const double x[4] * const struct0_T *s * double y[4] *返回类型:void */ void my_struct_inout(const double x[4], const struct0_T *s, double y[4]) {double b_y;B_y = ((s->f[0] + s->f[1]) + s->f[2]) + s->f[3];Y [0] = x[0] + b_y;Y [1] = x[1] + b_y;Y [2] = x[2] + b_y;Y [3] = x[3] + b_y;} /* * my_struct_inout.c * * [EOF] */

的生成函数签名my_struct_inout

(const double x[4], const struct0_T *s, double y[4])

my_struct_inout通过结构年代通过参考,即使PassStructByReference

相关的话题