我有一个 C++ 代码,我正在尝试与 Matlab 交互。我的 mex 文件在第一次运行时运行良好,但在第二次运行时崩溃。但是,如果我在执行之前清除 Matlab 中的所有变量(使用全部清除),程序永远不会崩溃。所以我有一个问题: 1. mex 函数能否在不使用某些特殊函数的情况下从 Matlab 工作区获取变量?我是否无意中在我的代码中这样做了?
- 我发布了我编写的 mex 函数。它有一个称为“块”的一维向量,可在名为 sphere_detector 的 C++ 函数中读取。对于目前的问题,块大小是 1x1920,它是在 sphere_detector 内的 16 个元素的块中读取的。当我读取 16 个元素的 SECOND 块时,程序崩溃了。我在块中读取的第一个元素将引发此错误:
MATLAB.exe 中 0x000007fefac7206f (sphere_decoder.mexw64) 处的第一次机会异常:0xC0000005:访问冲突读取位置 0xffffffffffffffff。MATLAB.exe 已触发断点
我检查了我的块向量,它应该已经初始化了所有的值并且它有。所以,我有点困惑为什么我会面临这个问题。
我正在使用 Matlab 2010a 和 Visual Studio 2010 Professional。
这是 mex 函数:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *mod_scheme, *Mt, *Mr, *block_length, *SNR;
mod_scheme = mxGetPr(prhs[0]);
Mt = mxGetPr(prhs[1]);
Mr = mxGetPr(prhs[2]);
block_length = mxGetPr(prhs[3]);
SNR = mxGetPr(prhs[4]);
/* Now take the input block. This is an encoded block and sphere detector will do the transmission too -- I can change it later */
double *block = mxGetPr(prhs[5]);
double *LIST_SIZE = mxGetPr(prhs[6]);
double **cand_sym;
int a = *mod_scheme;
int b = *Mt;
int c = *Mr;
int d = *block_length;
int e = *SNR;
int f = *LIST_SIZE;
int bitSize = (int)(log10(1.0*a)/log10(2.0));
for(int i=0; i<(int)*block_length; ++i)
{
printf("%d\n", (int)block[i]);
}
printf("Hello world %d %d %d %d %d!\n", (int)*mod_scheme, (int)*Mt, (int)*Mr, (int)*block_length, (int)*SNR);
/* Inputs are read correctly now set the outputs */
double *llr, *cand_dist;
/* for llrs */
plhs[0] = mxCreateDoubleMatrix(1, d, mxREAL);
llr = mxGetPr(plhs[0]);
/* for cand_dist */
int no_mimo_sym = d/(b*bitSize);
plhs[1] = mxCreateDoubleMatrix(1, f*no_mimo_sym, mxREAL);
cand_dist = mxGetPr(plhs[1]);
/* for cand_syms */
plhs[2] = mxCreateDoubleMatrix(b*bitSize*no_mimo_sym, f,mxREAL); //transposed version
double *candi;
candi = mxGetPr(plhs[2]);
cand_sym = (double**)mxMalloc(f*sizeof(double*));
if(cand_sym != NULL)
{
for(int i=0;i<f; ++i)
{
cand_sym[i] = candi + i*b*bitSize*no_mimo_sym;
}
}
sphere_decoder(a,b,c,d,e,block,f,llr,cand_dist,cand_sym);
// mxFree(cand_sym);
}
我得到读取异常的球体解码器代码中的部分如下所示:
for(int _block_length=0;_block_length<block_length; _block_length+=Mt*bitSize)
{
printf("Transmitting MIMO Symbol: %d\n", _block_length/(Mt*bitSize));
for(int _antenna = 0; _antenna < Mt; ++_antenna)
for(int _tx_part=0;_tx_part<bitSize; _tx_part++)
{
// PROGRAM CRASHES EXECUTING THIS LINE
bitstream[_antenna][_tx_part] = (int)block_data[_block_length + _antenna*bitSize + _tx_part];
}
............................REST OF THE CODE..................
}
任何帮助,将不胜感激。
关于新手