我已经使用编码器将一些 matlab 代码转换为 C++。
void myfunction(const emxArray_real_T *input, emxArray_real_T *output){ ... }
我已经设置好发送 emxArray_real_T
类型的输入,没有任何问题。如何设置以在调用 myfunction
的 C++ 中接收动态大小的输出?
代码更新:
main(){
.
.
.
double *inputVec;
inputVec=(double*)malloc(1000 * sizeof(double));
emxArray_real_T *input;
emxArray_real_T *output;
input=emxCreateWrapper_real_T(&inputVec[0],1,1000);
output = emxCreateWrapper_real_T(NULL,0,0);
myfunction(input,output);
emxDestroyArray_real_T(input);
emxDestroyArray_real_T(output);
.
.
}
这编译得很好但是错误的说
*** 检测到 glibc ***/data/myscript:双重释放或损坏 (!prev):0x000000000de54920 ***
最佳答案
您可以查看https://stackoverflow.com/a/24271438/3297440这似乎涵盖了类似的问题。
在这种特殊情况下,问题很可能是 output
指向的内存从未被初始化。您可以使用 myfunction_emxAPI.h
中的 emxCreate*
函数之一来初始化一个空的 emxArray
并将其传入。 之间的选择emxCreateWrapper_real_T
和 emxCreate_real_T
取决于您是否想要拥有为数据分配的内存。前者将所有权交给您,后者在 emxArray
拥有内存时使用。
类似于:
output = emxCreateWrapper_real_T(NULL,0,0);
在调用 myfunction
之前应该可以解决问题。
对了,别忘了打电话:
emxDestroyArray_real_T(input);
emxDestroyArray_real_T(output);
最后清理分配给 emxArrays
的所有内存。即使使用包装函数,也可以分配大小 vector 的存储空间。
关于c++ - Matlab C++ - 接收动态大小输出类型 (emxArray_real_T),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24662937/