从 C++-CLI 中的指针函数返回多维数组
我编写了以下代码从指针函数返回多维数组。该函数的输入参数是一维数组,输出是指向多维数组的指针。
double **function( array< double>^ data,int width,int height ) {
int i;
double **R = new double *[height];
for (i=0;i<=height;i++)
R[i]=new double [width];
// ....
return R;
}
int main( void ) {
int M=2, N=10, i,j;
// define multimensional array 2x10
array< array< double >^ >^ input = gcnew array< array< double >^ >(M);
for (j=0; j<input->Length; j++) {
input[j]=gcnew array<double>(N);}
double **result1 = new double *[N];
for(i=0; i<=N; i++)
result1[i]=new double [M];
double **result2 = new double *[N];
for(i=0; i<=N; i++)
result2[i]=new double [M];
//............
// send first row array of multidimensional array to function
result1=function(input[0],M,N);
// send second row array of multidimensional array to function
result2=function(input[1],M,N);
for (i=0;i<=N;i++)
delete R[k];
delete R;}*/
return 0;
}
我在 Visual Studio 2008 中成功构建了这个程序。当我调试此代码时,程序计算了 result1 pinter 变量,但在此处的函数中计算 result2 期间:
R=new double *[height];
for (i=0; i<=height; i++)
R[i]=new double [width];
Visual Studio 给出了此错误:
stdeneme.exe 中发生“System.Runtime.InteropServices.SEHException”类型的未处理异常
附加信息:外部组件抛出异常。
不幸的是我不知道该怎么办。
I wrote following code to return multidimensional array from pointer function.Input parameter of this function is one dimensional array, output is pointer that point multidimensional array.
double **function( array< double>^ data,int width,int height ) {
int i;
double **R = new double *[height];
for (i=0;i<=height;i++)
R[i]=new double [width];
// ....
return R;
}
int main( void ) {
int M=2, N=10, i,j;
// define multimensional array 2x10
array< array< double >^ >^ input = gcnew array< array< double >^ >(M);
for (j=0; j<input->Length; j++) {
input[j]=gcnew array<double>(N);}
double **result1 = new double *[N];
for(i=0; i<=N; i++)
result1[i]=new double [M];
double **result2 = new double *[N];
for(i=0; i<=N; i++)
result2[i]=new double [M];
//............
// send first row array of multidimensional array to function
result1=function(input[0],M,N);
// send second row array of multidimensional array to function
result2=function(input[1],M,N);
for (i=0;i<=N;i++)
delete R[k];
delete R;}*/
return 0;
}
I built this program succesfully in Visual Studio 2008.When I debug this code,the program computed result1 pinter variable but during computing result2 in the function here:
R=new double *[height];
for (i=0; i<=height; i++)
R[i]=new double [width];
Visual Studio give this error:
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in stdeneme.exe
Additional information: External component has thrown an exception.
Unfortunately I don't know what to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,我发现
您分配了 R[height] 一个错误
但循环的高度+1
你应该写循环
我看到的另一件事是,当你想破坏你的矩阵时,你写
但它应该是
At a glance I see one error
you have allocated R[height]
but the loop goes height+1
you should write the loop
Another thing I see is that when you want destroy your matrix you write
but it should be
<=
是您的问题。 有效的数组索引从0
到N-1
。 分配给result1[N]
是一种访问冲突 - 这就是它所抱怨的异常。The
<=
s are your problem. Valid array indices go from0
toN-1
. Assigning toresult1[N]
is an access violation - that's the exception it's complaining about.