繁体   English   中英

抛出异常:Visual Studio中Matlab Coder的访问冲突写入位置

[英]Exception thrown: Access violation writing location for Matlab Coder in Visual Studio

我有以下代码:

addstruct.h

#ifndef ADDSTRUCT_H
#define ADDSTRUCT_H

typedef struct {
    double* x;
    double* y;
} StructC;

void addstruct(double *a, double *b, const StructC *structc, int len);

#endif

addstruct.c

#include "addstruct.h"

void addstruct(double *a, double *b, StructC *structc, int len)
{
    for (int i=0; i<len; i++)
    {
        structc->x[i]=a[i]+b[i];
        structc->y[i]=-1*(a[i]+b[i]);
    }
}

calladdstruct.m

function S = calladdstruct(A,B)  %#codegen
    if coder.target('MATLAB')
    else
        coder.updateBuildInfo('addSourceFiles','addstruct.c');
        L=1000;
        Sx=zeros(1,L); Sy=zeros(1,L);
        StructC=struct('x',{Sx}, 'y',{Sy});
        coder.cstructname(StructC, 'StructC', 'extern', 'HeaderFile', 'addstruct.h');
        coder.ceval('addstruct', coder.rref(A), coder.rref(B), coder.ref(StructC), int32(numel(Sx)));
        S=sum(StructC.x);
        sprintf('sumx: %s', char( num2ascii(S,0) ))
        S=sum(StructC.y);
        sprintf('sumy: %s', char( num2ascii(S,0) ))
    end
end

和Visual Studio中的main.c

#include "calladdstruct.h"
#include "calladdstruct_initialize.h"
#include "calladdstruct_terminate.h"
#include <stdio.h>

int main()
{
   double S;
   double xarr[3]={1,2,3};
   double yarr[3]={1,2,3};
   calladdstruct_initialize();
   S = calladdstruct(xarr, yarr);
   printf("%f\n", S);
   calladdstruct_terminate();
   getchar();
   return 0;
}

当我在Visual Studio中运行它时,我收到错误

Exception Thrown at (calladdstruct.dll) in calladdstruct.exe: Access violation writing location

为什么是这样?

结构“StructC”的声明与MATLAB Coder所期望的不符。 尝试在coder.cstructname中生成不带“extern”参数的代码,以查看预期的类型。 然后在手写定义中使用它。

如果你需要一个特定的结构格式,那么使用coder.opaque和coder.ceval专门与它进行交互,这样你就不会在内存布局的假设中烘焙,这些假设必须在MATLAB Coder生成的数据和手写代码之间进行匹配。

希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM