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

2019-09-17 21:01发布

Im getting a runtime error of exc_bad_access ( code = 1, address=0x0) on line

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.

1条回答
混吃等死
2楼-- · 2019-09-17 21:33

Three things. 1.)The function arguments for y are int *& . Did you overload int with a bracket operator somewhere else? As specified, the int pointer should not have a []. 2.) Bracket operators are higher in precedence than a dereference operator. (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. Are they really necessary?

查看更多
登录 后发表回答