Main Content

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

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

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

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

MATLAB® Coder アプリを使用した参照渡しまたは値渡しの指定

[生成] ダイアログ ボックスを開くために、[コード生成] ページの [生成] 矢印をクリックします。

[ビルド タイプ] を次のいずれかに設定します。

  • ソース コード

  • スタティック ライブラリ

  • ダイナミック ライブラリ

  • 実行可能ファイル

[詳細設定] をクリックします。

[すべての設定] タブで [エントリポイント関数に構造体を参照で渡す] オプションを次のように設定します。

  • 参照渡しの場合は [はい] (既定値)

  • 値渡しの場合は [いいえ]

コマンド ライン インターフェイスを使用した参照渡しまたは値渡しの指定

スタティック ライブラリ、ダイナミック ライブラリまたは実行可能プログラム用のコード構成オブジェクトを作成します。たとえば、スタティック ライブラリ用のコード構成オブジェクトを作成します。

cfg = coder.config('lib'); 

PassStructByReference プロパティを次のように設定します。

  • 参照渡しの場合は true (既定値)

  • 値渡しの場合は false

以下に例を示します。

cfg.PassStructByReference = true;

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

入力の構造体引数をもつ 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
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: my_struct_in.c
 *
 * MATLAB Coder version            : 24.1
 * C/C++ source code generated on  : 25-Jan-2024 15:05:29
 */

/* 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
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: my_struct_in.c
 *
 * MATLAB Coder version            : 24.1
 * C/C++ source code generated on  : 25-Jan-2024 15:05:32
 */

/* 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
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: my_struct_out.c
 *
 * MATLAB Coder version            : 24.1
 * C/C++ source code generated on  : 25-Jan-2024 15:05:34
 */

/* 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
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: my_struct_out.c
 *
 * MATLAB Coder version            : 24.1
 * C/C++ source code generated on  : 25-Jan-2024 15:05:36
 */

/* 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
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: my_struct_inout.c
 *
 * MATLAB Coder version            : 24.1
 * C/C++ source code generated on  : 25-Jan-2024 15:05:38
 */

/* 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 を参照で渡します。

関連するトピック