编写Matlab C API的替代品以编写.matfiles

时间:2011-11-09 14:23:17

标签: c matlab c99

我正在研究模型,它将结果输出到matlab的.mat文件格式,并且最初与matlab库链接以使用其文件输出功能。 最近,需求发生了变化(谁已经猜到了),现在只需要在windows上编译以前的linux代码,最好不需要matlab来构建-but仍然输出.mat文件。
所以我搜索并找到了libmatio(http://sourceforge.net/projects/matio/)。虽然这很容易在linux中链接(你只是从存储库安装),但它在windows上很糟糕(基本上没有关于在windows上构建它的信息)。事实上,在1.3.3版本(早在2008年)中,Windows支持实际上是默默无闻的 此外,APi与matlab提供的完全不同,后者需要我重写/重构代码。

所以我想出了这个疯狂的想法...... 我需要直接替换Matlab API,最好不要使用库(为非程序员编译很容易),所以我开始编写一个。
我只实现了我需要的功能(编写双精度,字符串和复杂双精度数组,以及结构和结构嵌套)。所有这些都已经正常工作,除了一个:结构数组。

所有matlab数据都包含在一个名为'mxArray'的结构中,根据它的类型,它包含指向double,complex double或一个或多个其他mxArray的指针。
在将mxArray写入文件之前的最后一步是通过计算calcArraySize()来计算字节(以及它的子节点)的字节数。==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) 。 这会在某些时候导致段错误,因为我试图访问空指针。为了追踪原因,我通过valgrind运行代码。和往常一样,我会按照它们出现的顺序来处理任何问题,因为它们可能是后来发生的事情的原因。 所以valgrind告诉我的第一件事就是:

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;

注意:我在下面的代码中标记了[A]和[B] 结构定义(仅显示相关成员):

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;
}

我用来为structMatrix分配内存及其内容的函数:

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]
}

mxArrays还有两个其他分配函数:

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] <===============
}

此函数将一个mxArray指定为另一个的子节点:

//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,这会在我尝试访问时导致段错误。
怎么会这样?调用{{1}}时两者都被正确分配,那么这些指针如何重叠呢?我在俯瞰什么?

1 个答案:

答案 0 :(得分:2)

我认为问题出在你最后的陈述中:

mxSetFieldByNumber( mxStruct,   0,  /* THIRD FIELD */ 3,  mxSubStruct);

您正在尝试为mxStruct另一个嵌套结构变量分配第三个字段,问题是mxStruct仅定义了两个字段:

mxStruct = mxCreateStructMatrix(1, 1, /* TWO */ 2, fieldNames1);

与MATLAB不同,您的代码(据我所知)不支持动态添加结构字段:

%# -- MATLAB code --
s = struct('f1',[], 'f2',[]);
s.f3 = 99;       %# add a new field

这不是很难实现,只需重新分配指针数组以容纳一个字段并增加字段数。