读取 mex 文件中的结构数据时发生奇怪的事情

Strange goings on when reading structure data in mex file

本文关键字:数据 文件 mex 结构 读取      更新时间:2023-10-16

我刚才被一个非常奇怪的 mex 错误弄糊涂了......

将我的问题归结为核心,我们最终得到以下简单的 mex 代码。它只显示给定的结构字段是否为空......

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{    
    int numElements  = mxGetNumberOfElements(prhs[0]);
    int numFields = mxGetNumberOfFields(prhs[0]);
    mxArray* tmpData;
    const char* tmpName;
    for (int structIdx=0; structIdx<numElements; ++structIdx)
    {
        for (int fieldIdx=0; fieldIdx<numFields; ++fieldIdx)
        {
            tmpData = mxGetFieldByNumber(prhs[0], structIdx, fieldIdx);
            tmpName = mxGetFieldNameByNumber(prhs[0], fieldIdx);
            if (mxIsEmpty(tmpData))
                mexPrintf("struct(%i).%s is emptyn", structIdx+1, tmpName );
            else
                mexPrintf("struct(%i).%s contains datan", structIdx+1, tmpName );
        }
    }    
}

如果我们编译这段代码并将其称为structcrash那么下面的 matlab 代码......

clc
x.a=1;
x.b=2;
x(2).a=3;
x(2).b=4;
structcrash(x);

。给出了我们可能期望的输出...

  • struct(1(.a 包含数据
  • 结构(1(.b 包含数据
  • struct(2(.a 包含数据
  • struct(2(.b 包含数据

如果我们给 mex 函数一个包含空字段的结构,如下所示......

clc
y.a = [];
structcrash(y);

。然后我们也得到预期的输出...

  • 结构体(1(.a 为空

现在,如果你使用这样的代码,事情会变得非常奇怪......

clc
y(2).b = 4;
structcrash(y);

如果我们检查y结构,现在是 2 元素结构,每个元素中有 2 个字段。 y(1).a如上所述为空,并且在我们添加b字段时,y(1).b已被自动创建并被赋予空值。同样,当我们通过添加结构大小来增加结构大小时,y(2).a会自动创建y(2).b。该结构看起来完全合乎逻辑,但是用作 mex 文件的输入会导致段错误。

通过有选择地注释掉各种代码行,我可以确认导致段错误的命令是mxIsEmpty(tmpData)

其他人可以复制此错误吗,我在这里做错了什么吗?对我来说,它看起来像是 mex API 代码中的一个错误,但我想先在这里检查一下。谢谢

编辑:根据Heffernan的建议@David我修改了代码如下

        if(tmpData!=NULL) {
            if (mxIsEmpty(tmpData))
                mexPrintf("struct(%i).%s is emptyn", structIdx+1, tmpName );
            else
                mexPrintf("struct(%i).%s contains datan", structIdx+1, tmpName );
        }

。并且段错误不再发生。然而,这仍然非常不祥。如果创建两个结构(如以下示例所示(并使用工作区视图检查它们,则fg在各个方面看起来都完全相同。我找不到使用标准 matlab 编程命令它们的任何不同之处。

>> f(2).a=123;
>> g(1).a=[];
>> g(2).a=123

。但是whos命令揭示了差异...

  Name      Size            Bytes  Class     Attributes
  f         1x2               192  struct              
  g         1x2               296  struct 

。我更新的 MEX 函数显然也是如此......

>>structcrash(f)
struct(2).a contains data
>> structcrash(g)
struct(1).a is empty
struct(2).a contains data

因此,这个故事的寓意是,当您将新字段插入特定结构元素时,Matlab IDE 通过在所有结构中插入字段来使结构看起来漂亮和方形。然而,实际上,在底层内存中,情况并非如此。

小心!

正在发生的事情是mxGetFieldByNumber正在返回NULL然后您将其传递给mxIsEmpty,从而产生 seg 错误。文档指出,如果没有为指定字段分配值,mxGetFieldByNumber将返回NULL

要解决此问题,您需要防止将NULL传递给mxIsEmpty

if (tmpData == NULL || mxIsEmpty(tmpData))
    mexPrintf("struct(%i).%s is emptyn", structIdx+1, tmpName);
else
    mexPrintf("struct(%i).%s contains datan", structIdx+1, tmpName);