メインコンテンツ

生成されたコードで構造体の引数を参照または値で渡す

この例では、生成されたエントリポイント関数への構造体の引数が、参照渡しされるか値渡しされるかを制御する方法を示します。

参照渡しではポインターを使用して構造体の引数にアクセスします。入力された構造体の要素に関数が書き込みを行う場合、入力値は上書きされます。値渡しでは入力または出力の構造体引数のコピーが作成されます。メモリ使用量と実行時間を削減するには参照渡しを使用します。

構造体の引数が入力と出力の両方である場合には、生成されたエントリポイント関数は引数を参照で渡します。生成された MEX 関数は構造体の引数を参照で渡します。MEX 関数の出力では、構造体の引数を値渡しに指定できません。

スタンドアロン コード生成に対する参照渡しまたは値渡しの指定

既定では、コード ジェネレーターはスタンドアロン コードにおいて構造体の引数を参照で渡します。スタンドアロン コードを生成するときに、次のいずれかの方法を使用して、構造体の引数を値で渡すようにコード ジェネレーターに指示できます。

  • スタンドアロン コード構成オブジェクトで、PassStructByReference プロパティを false に設定する。

  • [コード生成設定] ダイアログ ボックスで、Pass structures by reference to entry-point functionsチェック ボックスをオフにする。

入力の構造体引数の参照渡し

入力の構造体引数をもつ MATLAB® 関数 my_struct_in を記述します。

type my_struct_in.m
function y = my_struct_in(s)
%#codegen

y = s.f;

MATLAB ワークスペースで構造体変数 mystruct を定義します。

mystruct = struct('f', 1:4);

C スタティック ライブラリ用のコード生成構成オブジェクトを作成します。

cfg = coder.config('lib');

構造体の引数を参照で渡すように指定します。

cfg.PassStructByReference = true;

コードを生成します。入力引数が変数 mystruct の型をもつように指定します。

codegen -config cfg -args {mystruct} my_struct_in
Code generation successful.

生成された C コードを表示します。

type codegen/lib/my_struct_in/my_struct_in.c
/*
 * File: my_struct_in.c
 *
 * MATLAB Coder version            : 25.1
 * C/C++ source code generated on  : 13-Jul-2025 16:32:30
 */

/* Include Files */
#include "my_struct_in.h"
#include "my_struct_in_types.h"

/* Function Definitions */
/*
 * Arguments    : const struct0_T *s
 *                double y[4]
 * Return Type  : void
 */
void my_struct_in(const struct0_T *s, double y[4])
{
  y[0] = s->f[0];
  y[1] = s->f[1];
  y[2] = s->f[2];
  y[3] = s->f[3];
}

/*
 * File trailer for my_struct_in.c
 *
 * [EOF]
 */

生成された my_struct_in の関数シグネチャは次のようになります。

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

my_struct_in は入力の構造体 s を参照で渡します。

入力の構造体引数の値渡し

構造体の引数を値で渡すように指定します。

cfg.PassStructByReference = false;

コードを生成します。入力引数が変数 mystruct の型をもつように指定します。

codegen -config cfg -args {mystruct} my_struct_in
Code generation successful.

生成された C コードを表示します。

type codegen/lib/my_struct_in/my_struct_in.c
/*
 * File: my_struct_in.c
 *
 * MATLAB Coder version            : 25.1
 * C/C++ source code generated on  : 13-Jul-2025 16:32:38
 */

/* Include Files */
#include "my_struct_in.h"
#include "my_struct_in_types.h"

/* Function Definitions */
/*
 * Arguments    : const struct0_T s
 *                double y[4]
 * Return Type  : void
 */
void my_struct_in(const struct0_T s, double y[4])
{
  y[0] = s.f[0];
  y[1] = s.f[1];
  y[2] = s.f[2];
  y[3] = s.f[3];
}

/*
 * File trailer for my_struct_in.c
 *
 * [EOF]
 */

生成された my_struct_in の関数シグネチャは次のようになります。

void my_struct_in(const struct0_T s, double y[4]

my_struct_in は入力の構造体 s を値で渡します。

出力の構造体引数の参照渡し

出力の構造体引数をもつ MATLAB 関数 my_struct_out を記述します。

type my_struct_out.m
function s = my_struct_out(x)
%#codegen

s.f = x;

MATLAB ワークスペースで変数 a を定義します。

a = 1:4;

C スタティック ライブラリ用のコード生成構成オブジェクトを作成します。

cfg = coder.config('lib');

構造体の引数を参照で渡すように指定します。

cfg.PassStructByReference = true;

コードを生成します。入力引数が変数 a の型をもつように指定します。

codegen -config cfg -args {a} my_struct_out
Code generation successful.

生成された C コードを表示します。

type codegen/lib/my_struct_out/my_struct_out.c
/*
 * File: my_struct_out.c
 *
 * MATLAB Coder version            : 25.1
 * C/C++ source code generated on  : 13-Jul-2025 16:32:42
 */

/* Include Files */
#include "my_struct_out.h"
#include "my_struct_out_types.h"

/* Function Definitions */
/*
 * Arguments    : const double x[4]
 *                struct0_T *s
 * Return Type  : 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];
}

/*
 * File trailer for my_struct_out.c
 *
 * [EOF]
 */

生成された my_struct_out の関数シグネチャは次のようになります。

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

my_struct_out は出力の構造体 s を参照で渡します。

出力の構造体引数の値渡し

構造体の引数を値で渡すように指定します。

cfg.PassStructByReference = false;

コードを生成します。入力引数が変数 a の型をもつように指定します。

codegen -config cfg -args {a} my_struct_out
Code generation successful.

生成された C コードを表示します。

type codegen/lib/my_struct_out/my_struct_out.c
/*
 * File: my_struct_out.c
 *
 * MATLAB Coder version            : 25.1
 * C/C++ source code generated on  : 13-Jul-2025 16:32:44
 */

/* Include Files */
#include "my_struct_out.h"
#include "my_struct_out_types.h"

/* Function Definitions */
/*
 * Arguments    : const double x[4]
 * Return Type  : struct0_T
 */
struct0_T 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];
  return s;
}

/*
 * File trailer for my_struct_out.c
 *
 * [EOF]
 */

生成された my_struct_out の関数シグネチャは次のようになります。

struct0_T my_struct_out(const double x[4])

my_struct_out は出力の構造体を返します。

入力および出力の構造体引数の参照渡し

引数が入力と出力の両方である場合には、PassStructByReference が false であっても生成された C 関数は引数を参照で渡します。

入力引数でもあり出力引数でもある構造体引数をもつ MATLAB 関数 my_struct_inout を記述します。

type my_struct_inout.m
function [y,s] = my_struct_inout(x,s)
%#codegen

y = x + sum(s.f);

MATLAB ワークスペースで変数 a と構造体変数 mystruct を定義します。

a = 1:4;
mystruct = struct('f',a);

C スタティック ライブラリ用のコード生成構成オブジェクトを作成します。

cfg = coder.config('lib');

構造体の引数を値で渡すように指定します。

cfg.PassStructByReference = false;

コードを生成します。最初の入力は a の型を、2 番目の入力は mystruct の型をもつように指定します。

codegen -config cfg -args {a, mystruct} my_struct_inout
Code generation successful.

生成された C コードを表示します。

type codegen/lib/my_struct_inout/my_struct_inout.c
/*
 * File: my_struct_inout.c
 *
 * MATLAB Coder version            : 25.1
 * C/C++ source code generated on  : 13-Jul-2025 16:32:47
 */

/* Include Files */
#include "my_struct_inout.h"
#include "my_struct_inout_types.h"
#include <emmintrin.h>

/* Function Definitions */
/*
 * Arguments    : const double x[4]
 *                const struct0_T *s
 *                double y[4]
 * Return Type  : void
 */
void my_struct_inout(const double x[4], const struct0_T *s, double y[4])
{
  __m128d r;
  r = _mm_set1_pd(((s->f[0] + s->f[1]) + s->f[2]) + s->f[3]);
  _mm_storeu_pd(&y[0], _mm_add_pd(_mm_loadu_pd(&x[0]), r));
  _mm_storeu_pd(&y[2], _mm_add_pd(_mm_loadu_pd(&x[2]), r));
}

/*
 * File trailer for my_struct_inout.c
 *
 * [EOF]
 */

生成された my_struct_inout の関数シグネチャは次のようになります。

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

PassStructByReferencefalse であっても、my_struct_inout は構造体 s を参照で渡します。

参考

| |

トピック