简体   繁体   English

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

[英]c++/error :: exc_bad_access error code=1

Im getting a runtime error of exc_bad_access ( code = 1, address=0x0) on line 我在行上获得了exc_bad_access(代码= 1,地址= 0x0)的运行时错误

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

in the summation function. 在求和函数中。 I know the problem is not a memory leak, so i don't quite know how to go about solving this problem. 我知道问题不是内存泄漏,所以我不太了解如何解决此问题。

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);


}

Thank you for the help. 感谢您的帮助。

Three things. 三件事。 1.)The function arguments for y are int * & . 1.)y的函数参数为int * &。 Did you overload int with a bracket operator somewhere else? 您是否在其他地方用方括号运算符重载了int? As specified, the int pointer should not have a []. 如指定的那样,int指针不应包含[]。 2.) Bracket operators are higher in precedence than a dereference operator. 2.)括号运算符的优先级高于取消引用运算符。 (Almost always a good idea to enclose them within parenthesis). (将它们放在括号内几乎总是一个好主意)。 The way this is written, the bracket operator will be performed before the deref. 编写方式,方括号运算符将在反引用之前执行。 3.) It seems unusual that you should need so many dereference operators. 3.)您似乎需要很多取消引用运算符似乎很不寻常。 Are they really necessary? 他们真的有必要吗?

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

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