因此,我搜索并找到了libmatio(http://sourceforge.net/projects/matio/)。虽然在Linux上很容易链接(只需从存储库中安装即可),但在Windows上非常糟糕(基本上没有任何有关在Windows上构建它的信息)。实际上,似乎在2008年版本1.3.3中已经悄悄地放弃了对Windows的支持。
此外,APi与Matlab提供的完全不同,这将要求我重写/重新结构化大量代码。
所以我想出了这个疯狂的想法......
我需要一个Matlab API的插入替换,最好不使用库(以使非程序员轻松编译),因此我开始编写一个。
我只实现了我需要的功能(编写双倍、字符串和复杂双倍的数组,以及结构和结构嵌套)。所有这些都已经正常工作,除了一个:结构数组。
所有Matlab数据都包含在一个称为“mxArray”的结构中,根据其类型,它包含指向double、complex double或一个或多个其他mxArray的指针。
在将mxArray写入文件之前的最后一步是通过调用calcArraySize()
计算其字节大小(以及其子级的大小)。这会导致某些时候发生段错误,因为我正在尝试访问空指针。为了找出原因,我通过valgrind运行了代码。像往常一样,我尝试按照出现的顺序解决任何问题,因为它们可能是后面发生的事情的原因。
所以valgrind告诉我的第一件事是:
==8405== Invalid write of size 8
==8405== at 0x00404541: mxSetFieldByNumber (mxSetFieldByNumber.c:18) [A]
==8405== by 0x00411679: calcAllRayInfo (calcAllRayInfo.c:156)
==8405== by 0x0041dd42: main (cTraceo.c:111)
==8405== Address 0x5500250 is 0 bytes inside a block of size 4 alloc'd
==8405== at 0x04c28f9f: malloc (vg_replace_malloc.c:236)
==8405== by 0x00401066: mallocChar (toolsMemory.c:69)
==8405== by 0x00404314: mxCreateStructMatrix (mxCreateStructMatrix.c:43) [B]
==8405== by 0x00411235: calcAllRayInfo (calcAllRayInfo.c:105)
==8405== by 0x0041dd42: main (cTraceo.c:111)
注意:在下面的代码中,我标记了 [A] 和 [B]。
结构定义(仅显示相关成员):
struct mxArray{
bool isStruct; //determines if this mxArray is a structure (which contains other mxArrays)
bool isChild; //determines wheter this mxArray is a Child of another (when set, its name will not be written to the matfile, as it is already defined in the parent's fieldnames
uintptr_t nFields;
char **fieldNames; //something like: {"theta","r","z"};
struct mxArray **field; //pointer to member mxArrays. only used when isStruct is set.
};
typedef struct mxArray mxArray;
我使用的函数用于为structMatrix分配内存以及其内容:
mxArray* mxCreateStructMatrix(uintptr_t nRows, uintptr_t nCols, uintptr_t nFields, const char **fieldNames){
/*
* creates a 2D array of structures
*/
mxArray* outArray = NULL;
/* do some input value validation */
// allocate memory
outArray = malloc(nRows*nCols*sizeof(mxArray));
if (outArray == NULL){
fatal("mxCreateStructMatrix(): memory allocation error.");
}
// allocate memory for structure members (fields)
for (uintptr_t iStruct=0; iStruct<nCols*nRows; iStruct++){
outArray[iStruct].nFields = nFields;
outArray[iStruct].fieldNames = malloc(nFields*sizeof(char*));
//copy fieldnames into struct info
for (uintptr_t iField=0; iField<nFields; iField++){
//NOTE: strlen returns length of string not including the terminating NULL character
outArray[iStruct].fieldNames[iField] = mallocChar(strlen(fieldNames[iField])+1); // [B] <=======
strncpy(outArray[iStruct].fieldNames[iField], fieldNames[iField], strlen(fieldNames[iField]));
}
outArray[iStruct].field = NULL;
outArray[iStruct].field = malloc(nFields*sizeof(mxArray*));
if (outArray[iStruct].field == NULL){
fatal("mxCreateStructMatrix(): memory allocation error.\n");
}
}
return outArray;
}
mxArray还存在另外两个分配函数:
mxArray* mxCreateDoubleMatrix(uintptr_t nRows, uintptr_t nCols, uintptr_t numericType){
/*
* creates a 2D array of double precision floating point values.
* can be real or complex.
*/
[snip]
}
mxArray* mxCreateString(const char *inString)
/*
* creates an mxArray containing a string.
*/
[snip]
}
这个函数将一个 mxArray 分配为另一个 mxArray 的子项:
void mxSetFieldByNumber(mxArray* mxStruct, //pointer to the mxStruct
uint32_t index, //linear index of the element
uint32_t iField, //index of the structure's field which we want to set.
mxArray* inArray){ //the mxArray we want to assign to the mxStruct
/*
* Assigns an mxArray to one of the fields of a structArray
*/
inArray->isChild = true; //determines that this mxArray is a child of another one
mxStruct[index].field[iField] = inArray; // [A] <===============
}
使用方法如下:
//create parent mxArray:
mxStruct = mxCreateStructMatrix(1, //number of rows
1, //number of columns
2, //number of fields in each element
fieldNames1); //list of field names
//create children:
mxY = mxCreateDoubleMatrix(1 ,1, mxREAL);
mxZ = mxCreateDoubleMatrix(1 ,1, mxREAL);
mxSubStruct = mxCreateStructMatrix(1, //number of rows
1, //number of columns
3, //number of fields in each element
fieldNames2); //list of field names
/* copy some values into the mxArrays */
[snip]
//link children to parents
mxSetFieldByNumber( mxStruct, //pointer to the parent mxArray
0, //index of the element (linear)
0, //position of the field (in this case, field 0 is "w"
mxY); //the mxArray we want to add to the mxStruct
mxSetFieldByNumber( mxStruct, 0, 1, mxZ);
mxSetFieldByNumber( mxSubStruct, 0, 0, mxY);
mxSetFieldByNumber( mxSubStruct, 0, 1, mxZ);
mxSetFieldByNumber( mxStruct, 0, 2, mxSubStruct);
显然,mxStruct[index].field[iField] = inArray;
正在写入mxStruct[index].fieldNames
,从而使mxStruct[index].field[iField] == NULL
,这会导致我尝试访问它时出现段错误。
这是怎么回事?在调用mxCreateStructMatrix
时,两者都被正确分配,那么这些指针如何重叠?我忽略了什么?