c++ - Matlab C++ - 接收动态大小输出类型 (emxArray_real_T)

标签 c++ matlab matlab-coder

我已经使用编码器将一些 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_TemxCreate_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/

相关文章:

c++ - 如何使用另一个变量声明一个变量?

matlab - 在一个图中对齐轴

arrays - 在数组中查找和乘以重复值的最快方法

c++ - 使用 Matlab Coder 将 Matlab m 文件转换为 C/C++ 代码,包括 mex 文件 (mxArray)

c++ - 是否可以将 "sub"标签插入 LLVM 中的 BasicBlock?

python - 从 C++ 中在本地范围内创建 Python 对象

c++ - 在 C++ 中设置实现?

python - 从 python 运行 MATLAB 脚本 + 传递参数

c - Matlab编码器: Matlab Array to C Array

c++ - 在 Visual Studio 或 g++ 上运行 Matlab Coder 输出项目