使用指针的问题 [英] problem using pointers

查看:67
本文介绍了使用指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个代码来计算c#中数组的求逆.现在,我已将其转换为c ++,但运行时,它显示"math.exe中0x004011a0处未处理的异常:0xC0000005:读取访问位置0xabababb3的访问冲突"消息,它退出了程序.我认为这与我用作2D数组的指针有关.在我的主要函数中,我使用2D指针参数调用Invert函数.请帮忙.

代码:

I''ve written a code to calculate the invert of an array in c#. Now I''ve converted it to c++, but when I run, it shows "Unhandled exception at 0x004011a0 in math.exe: 0xC0000005: Access violation reading location 0xabababb3" message and it exits the program. I think it''s about pointers i used as 2D arrays. in my main function I call the Invert function with a 2D pointer parameter. Please help.

Code:

CDoubleMatrix::CDoubleMatrix(int row,int col)
{
    this->row=row;
    this->col=col;
}

void CDoubleMatrix::makeResult()
{
    result=new double*[row];
    for (int i=0;i<row;i++)
        result[i]=new double[col];
}

double** CDoubleMatrix::DevideByConst(double** mtx, double val)
{
    makeResult();

    for (int i=0;i<row;i++)
    {
        for (int j=0;j<col;j++)
        {
            result[i][j]=mtx[i][j]/val;
        }
    }
    return result;
}

double** CDoubleMatrix::Invert(double** mtx)
{
    if (Determinant(mtx) == 0)
    {

    }
    if (row == 2 && col == 2)
    {
        double** tempMtx=new double*[2];
        for (int i=0;i<2;i++)
            tempMtx[i]=new double[2];

        tempMtx[0][0] = mtx[1][1];
        tempMtx[0][1] = -mtx[0][1];
        tempMtx[1][0] = -mtx[1][0];
        tempMtx[1][1] = mtx[0][0];

        return DevideByConst(tempMtx, Determinant(mtx));
    }
    return DevideByConst(Adjoint(mtx), Determinant(mtx));
}

double CDoubleMatrix::Determinant(double** mtx)
{
    double determinent = 0;

    if (row != col)
    {

    }
    if (row == 1 && col == 1)
    {
    return mtx[0][0];
    }
    if (row == 2 && col == 2)
    {
        determinent = (mtx[0][0]* mtx[1][1])- (mtx[0][1]* mtx[1][0]);
        return determinent;
    }

    double** tempMtx=new double*[row-1];
    for (int i=0;i<row-1;i++)
        tempMtx[i]=new double[col-1];

    for (int j = 0; j < col; j++)
    {
        tempMtx = Minor(mtx, 0, j);
        determinent += pow((double)-1, (double)j)*(mtx[0][j]* Determinant(tempMtx));
    }

    return determinent;
}

double** CDoubleMatrix::Minor(double **mtx, int row, int column)
{
    if (this->row < 2 || this->col < 2)
    {

    }

    int i, j = 0;

    double** minorMtx=new double*[this->row-1];
    for (int i=0;i<this->row-1;i++)
        minorMtx[i]=new double[this->col-1];

    for (int k = 0; k < this->row - 1; k++)
    {
        if (k >= row)
            i = k + 1;
        else
            i = k;

        for (int l = 0; l < this->col - 1; l++)
        {
            if (l >= column)
                j = l + 1;
            else
                j = l;

            minorMtx[k][l] = mtx[i][j];
        }
    }
    return minorMtx;
}

double** CDoubleMatrix::Adjoint(double** mtx)
{
    if (row < 2 || col < 2)
    {

    }

    double** tempMtx=new double*[row-1];
    for (int i=0;i<row-1;i++)
        tempMtx[i]=new double[col-1];

    double** adjMtx=new double*[row];
    for (int i=0;i<row;i++)
        adjMtx[i]=new double[col];

    for (int i=0;i<row;i++)
    {
        for (int j=0;j<col;j++)
        {
            tempMtx = Minor(mtx, i, j);
            adjMtx[j][i] = pow((double)-1, (double)(i + j)) * Determinant(tempMtx);
        }
    }
    return adjMtx;
}



更新
主要功能:



Update
In main function:

double** r=new double*[10];
for (int i=0;i<10;i++)
    r[i]=new double[10];

double** m=new double*[10];
for (int i=0;i<10;i++)
    m[i]=new double[10];

for (int i=0;i<10;i++)
    for (int j=0;j<10;j++)
    m[i][j]=2;


CDoubleMatrix dm(10,10);

r=dm.Invert(m);

for (int i=0;i<10;i++)
    for (int j=0;j<10;j++)
        cout<<r[i][j]<<endl;

推荐答案

您定义了DoubleMatrix,但没有定义初始化它(我假设这是函数DoubleMatrix::MakeResult()的作用).结果,您的成员变量DoubleMatrix::result是未定义的.它可能是非null值,因此,如果您访问它,则可能会发生访问冲突.

[edit]检查您的代码,直到您报告该异常为止,我都没有看到您访问结果,因此它必须是其他内容.[/edit]

[edit2]
好吧,看看您的代码,它的作用是:

1.分配一个10x10矩阵r和另一个10x10矩阵m.
2.将m的所有系数初始化为2(这意味着行列式将为0 btw)
3.它创建一个类型为CDoubleMatrix的对象,其成员变量设置为row=col=10result = 未定义
4.在这个新对象上,将m传递给函数Invert(),该函数将其传递给Determinant()
5.经过一些检查,您分配了一个新的9x9矩阵tempMtx
6.然后运行一个循环,在其中重新分配tempMtx 10次,创建10个(内存)泄漏矩阵!但是,内存泄漏不会导致运行时异常.要重新分配tempMtx,请调用Minor(),然后再次递归调用Determinant().
Minor()中的6.1中,您分配了一个9x9矩阵,并(正确吗?)将一个9x9子矩阵复制到其中以计算行列式.
Determinant()中的6.2中,您再次创建9x9 tempMtx(请参阅步骤5),然后运行循环,将其重新分配给从对Minor()的循环调用中获得的任何内容(如果返回的话)(请参阅步骤6和6.1):
7.对Minor()的递归调用再次分配9x9矩阵(这次应该为8x8!),然后尝试从传递的mtx复制子矩阵,假设其尺寸为10x10.不幸的是,它是9x9,所以您将获得内存访问异常!
[/edit2]
You defined your DoubleMatrix, but didn''t initialize it ( I assume that is what the function DoubleMatrix::MakeResult() is for). As a result, your member variable DoubleMatrix::result is undefined,. It may be a non-null value, so if you acces it you may get an access violation.

[edit]checking your code I didn''t see you accessing result unto the point you reported the exception, so it has to be something else.[/edit]

[edit2]
Ok, looking at your code, what it does is this:

1. It allocates a 10x10 matrix r and another 10x10 matrix m.
2. It initalizes all the coefficients of m to 2 (which means the determinant will be 0 btw)
3. It creates an object of type CDoubleMatrix, with its member variables set to row=col=10 and result=undefined
4. On this new object, you pass m to the function Invert(), which passes it on to Determinant()
5. After some checks, you allocate a new 9x9 matrix tempMtx
6. then you run a loop where you reassign tempMtx 10 times, creating 10 (memory-)leaking matrices! However, memory leaks don''t cause run-time exceptions. To re-assign tempMtx, you call Minor(), then recursively call Determinant() again.
6.1 in Minor(), you allocate a 9x9 matrix, and (correctly?) copy a 9x9 submatrix for calculating the determinant into it.
6.2 in Determinant() you again create a 9x9 tempMtx (see step 5), then run a loop re-assigning it to whatever you would get from your looped call to Minor(), if it would return (see step 6 and 6.1):
7. The recursive call to Minor() again allocates a 9x9 matrix (should be 8x8 this time!), then attempts to copy a submatrix from the passed mtx, assuming that it is of the dimension 10x10. Unfortunately, it is 9x9 however, so you get memory access exceptions instead!
[/edit2]


看看您的代码:
result[i][j]=mtx[i][j]/val;
mtx的位置为:
double** mtx
编译器不知道二维数组的行尺寸.
因此,您应该对double数组使用类型声明.这样编译器就可以看到mtx参数的尺寸.
typedef double MAT3x3[3][3];
因此您可以致电:
DevideByConst(MAT3x3& mtx, double val)
最好的问候.
Take a look at your code:
result[i][j]=mtx[i][j]/val;
where mtx comes as:
double** mtx
the compiler dont know the row dimensions of the 2-dimensional array.
therefore you should use a typedeclaration for the double array. so the compiler can see the dimensions of mtx parameter.
typedef double MAT3x3[3][3];
so you can call:
DevideByConst(MAT3x3& mtx, double val)
Best regards.


这是猜测,但可能是问题所在.

您尚未提供给我们的一条信息是您最初用于调用Invert()函数的参数.我怀疑您正在使用定义如下的静态数组:
This is guesswork, but may be what the problem is.

The one piece of information that you have not given us is the parameter that you are using to call the Invert() function in the first place. My suspicion is that you are using a static array defined something like:
double matrix[2][2] =
{
    { 2.0, 3.0 },
    { 4.0, 6.0 },
};
double** result = Invert(double**)matrix);


但是在这种情况下,matrix[0]matrix[1]不是指向double数组的指针,而是实际的数组.因此,当Invert()(或任何其他函数)尝试使用它认为的指针时,由于内容无效,它会导致访问错误.


But in this case matrix[0] and matrix[1] are not pointers to arrays of doubles, but actual arrays. So, when Invert() (or any other function) tries to use what it thinks is the pointer, it causes an access error because the content is invalid.


这篇关于使用指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆