读取 mex 文件中的结构数据时发生奇怪的事情
Strange goings on when reading structure data in mex file
我刚才被一个非常奇怪的 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 );
}
。并且段错误不再发生。然而,这仍然非常不祥。如果创建两个结构(如以下示例所示(并使用工作区视图检查它们,则f
和g
在各个方面看起来都完全相同。我找不到使用标准 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);
- C++数据文件、数组和计算赋值
- 数据文件的第二行未正确读取
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 将数据文件读入结构 C++
- 引入流时C++数据文件未正确读取?
- 如何在数据文件中查找和显示内容
- 如何处理错误"E1696命令行错误:无法在Visual Studio 2017中打开元数据文件"mscorlib.dll"?
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 将数据文件读入对象数组时出现问题
- C :数据文件有错误:预期的无限制ID
- C 和 C++:带有错误"Expected unqualified-id"的数据文件
- 如何在C 中读取一系列数据文件
- 从数据文件中读取每2个字节,并在向量中进行比较
- 写入单个大数据文件或多个小文件:哪个更快?
- 在读取数据文件时,如何找到用户字符串输入的字谜?
- 保护外部数据文件免受未经授权的修改
- C 读取数据文件
- C 如何将数据文件读取到结构或向量以及返回结构或向量
- 具有挑战性的数据文件格式,需要将其读取为包含类对象的数组的VAR
- C 中的Google单元测试:如何编写持久数据文件