c++/error::exc_bad_access错误代码=1

c++/error :: exc_bad_access error code=1

本文关键字:错误代码 access bad exc c++ error      更新时间:2023-10-16

我在线上得到一个运行时错误exc_bad_access(代码=1,地址0x0)

asize = **y[0] + **y[1];

在求和函数中。我知道这个问题不是内存泄漏,所以我不太知道如何解决这个问题。

void allocArr (int **&x, int ***&y, int **&q, int ****&z)
{
    x = new int *[2];
    y = new int **(&*x);
    q = &*x;
    z = new int ***(&q);
}
void summation(int ***&y, int arr[])
{
    int asize = 0;
    asize = **y[0] + **y[1];
    **y[2] = *new int [asize];
    *(arr + 2) = asize;
}
void putArr(int **&x, const int &size1,const int &size2)
{
    x[0] = *new int* [size1];
    x[1] = *new int* [size2];
}
int main()
{
    int size1, size2;
    int a = 1, b = 2;
    int** x;
    int*** y;
    int** q;
    int**** z;
    int arr[2];
    allocArr(x, y, q, z);
    Input(x, arr, size1, size2, a, b);
    summation(y, arr);
    display(z);

}

谢谢你的帮助。

三件事。1.)y的函数参数是int*&你在其他地方用括号运算符重载int了吗?根据指定,int指针不应具有[]。2.)括号运算符的优先级高于取消引用运算符。(把它们放在括号里几乎总是个好主意)。按照这种编写方式,括号运算符将在取消引用之前执行。3.)你需要这么多解引用运算符,这似乎很不寻常。它们真的有必要吗?