如何释放mxGetData()分配的内存

艾莉亚·克拉克(Aliya Clark)

我正在将mat文件导入我的C ++代码。导入数据后,执行计算并将其保存到另一个位置,我想释放原始数据占用的内存。

有没有执行此功能的特定功能。是否只删除mxGetData()释放内存返回的指针

这是我创建的用于导入Mat文件的类

#ifndef READMAT_H
#define READMAT_H
#include "mat.h"
#include "matrix.h"
#include "mex.h"
#include "program_exception.h"
#include "stdint.h"

class readmat
{
private:
    const size_t *dimarray;
    const char **dir;
    MATFile *pmat;
    int ndir;
    mxArray *painfo,*pa;
    const char *file;
    int minute;
    int second;
    const char *name;
    const char *name1;
    bool isdouble;
    no_mat_exception noMAT;
public:

    //with default value
    readmat(const char *f);
    //  get number of dimensions
    int getnumbrofdimensions() const;
    // get pointer to array
    void*  getarraypointer() const;
    // get pointer to each dimension size
    const size_t* dimensionpointer() const;
    //number of elements
    int numberofelements() const;
    ~readmat();

};

#endif

以下是cpp的实现

#include "readmat.h"
#include <iostream>
#include "mat.h"
#include "matrix.h"
#include "mex.h"
using namespace std;

// set the file name
readmat::readmat(const char *f)
{
    file = f;
    pmat = matOpen(file, "r");
    if (pmat == NULL) {
        throw noMAT;
    }
    else
    {
        dir = (const char **)matGetDir(pmat, &ndir);
        if (dir == NULL) {
            printf("Error reading directory of file %s\n", file);

        }
        else if (ndir > 1)
        {
            cout << "The number of variables are larger than 1" << endl;
        }
        else
        {
            mxFree(dir);
            matClose(pmat);
            pmat = matOpen(file, "r");
            if (pmat == NULL) {
                throw noMAT;
            }
            else
            {
                painfo = matGetNextVariableInfo(pmat, &name);
                matClose(pmat);
            }
            pmat = matOpen(file, "r");
            if (pmat == NULL) {
                throw noMAT;
            }
            else
            {
                pa = matGetNextVariable(pmat, &name1);
                matClose(pmat);
            }
        }

    }

}



int readmat::getnumbrofdimensions() const
{

    return mxGetNumberOfDimensions(painfo);
}

void* readmat::getarraypointer() const
{
    //return mxGetPr(pa);
    return mxGetData(pa);
}

const size_t*  readmat::dimensionpointer() const
{
    return mxGetDimensions(painfo);
}

int readmat::numberofelements() const
{
    return mxGetNumberOfElements(painfo);
}
readmat::~readmat()
{
    mxFree(pa);
    mxFree(painfo);

}

在这里,当我删除目标程序时,在文件free.c中触发一个断点

安德·比古里(Ander Biguri)

上的MATLAB演示edit([matlabroot '/extern/examples/eng_mat/matdgns.c']);似乎建议使用mxDestroyArray而不是mxFree

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章