简体   繁体   English

指针数组的C ++错误:: EXC_BAD_ACCESS

[英]c++ error :: EXC_BAD_ACCESS for pointer arrays

I keep getting the error message, exc_bad_access code=1 for my line 我不断收到错误消息,我的行的exc_bad_access code = 1

asize = *(***(y) + **(y + 1));

in the summation function. 在求和函数中。 I dont quite understand what to do with this error, but i know that it is not a memory leak. 我不太了解如何处理此错误,但我知道这不是内存泄漏。 I am trying to get the values stored in the y pointer array, add them, and store it in the variable asize. 我试图获取存储在y指针数组中的值,添加它们,然后将其存储在变量asize中。

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 putArr(int **&x, int &size1, int &size2)
{
    *(x) = *new int* [size1];

    *(x + 1) = *new int* [size2];

}

void Input (int **&x, int *&arr, int &size1,int &size2, int a, int b)
{

    cout << "Please enter 2 non-negative integer values: "<< endl;

    checkVal(size1, a);
    checkVal(size2, b);
    putArr(x, size1, size2);

    arr[0] = size1;
    arr[1] = size2;

    cout << x[0];
}


void summation(int ***&y, int *&arr)
{
    int asize = 0;
    asize = *(***(y) + **(y + 1));
    **y[2] = *new int [asize];

    *(arr + 2) = asize;

}

int main()
{
    int size1, size2;
    int a = 1, b = 2;

    int** x;
    int*** y;
    int** q;
    int**** z;

    int *arr = new int [2];

    allocArr(x, y, q, z);
    Input(x, arr, size1, size2, a, b);
    summation(y, arr);
    display(z);


}

Thank you for the help. 感谢您的帮助。 Im really struggling here... 我真的在这里挣扎...

Not sure how you got started with the code. 不确定如何开始使用代码。 The code can be simplified quite a bit to help you, and readers of your code, understand what's going on. 该代码可以简化很多,以帮助您,您的代码读者也可以了解发生了什么。

Function allocArr 函数allocArr

The lines 线

y = new int **(&*x);
q = &*x;

can be

y = new int **(x);  // &*x == x
q = x;

Function putArr 函数putArr

You have the function declaration as: 您的函数声明为:

void putArr(int **&x, int &size1, int &size2)

It can be changed to: 可以更改为:

void putArr(int **x, int size1, int size2)

without changing how you are using the variables. 无需更改变量的使用方式。

Your code in the function seems strange. 您在函数中的代码似乎很奇怪。 Did you mean for x[0] and x[1] to point to an array of size1 and size2 int s, respectively? 您是说x[0]x[1]分别指向size1size2 int s数组吗? If you did, the code would be: 如果这样做,代码将是:

x[0] = new int[size1];
x[1] = new int[size2];

If you don't mean the above, it's hard to figure out what you are trying to do with your code. 如果您的意思不是上面的意思,那么很难弄清楚您要如何使用代码。

Function Input 功能Input

You have the function declaration as: 您的函数声明为:

void Input (int **&x, int *&arr, int &size1,int &size2, int a, int b)

It can be changed to: 可以更改为:

void Input (int **x, int *arr, int &size1,int &size2, int a, int b)

without changing how you are using the variables. 无需更改变量的使用方式。

You are calling a function checkVal , but your posted code doesn't have that function. 您正在调用一个函数checkVal ,但是您发布的代码没有该函数。 It's not clear what that function is doing. 目前尚不清楚该功能在做什么。 You have the line 你有线

cout << "Please enter 2 non-negative integer values: "<< endl;

just before the calls to checkVal . 就在调用checkVal之前。 Presumably, checkVal reads the input and stores them in size1 in the first call and size2 in the second call. 据推测, checkVal读取输入并将其存储在size1在第一个呼叫, size2在第二个呼叫。 It's not clear how the second argument to checkVal is used. 目前尚不清楚如何使用checkVal的第二个参数。

And then, you have the line: 然后,您可以看到:

cout << x[0];

It's not clear what you wish to accomplish from printing an int* to cout . 从打印int*cout尚不希望完成什么。 Perhaps it was part of your debugging code. 也许这是您的调试代码的一部分。 The line doesn't change anything else in the program. 该行不会更改程序中的其他任何内容。 It's just strange to see it there. 在那里看到它真是奇怪。

Function summation 功能summation

You have the function declaration as: 您的函数声明为:

void summation(int ***&y, int *&arr)

It can be changed to: 可以更改为:

void summation(int ***y, int *arr)

without changing how you are using the variables. 无需更改变量的使用方式。

In this function, you have the expression: 在此函数中,您具有以下表达式:

asize = *(***(y) + **(y + 1));

What do you get when you evaluate ***(y) ? 评估***(y)什么?

***(y) = **(*y) = **(x) = *(*x) = *(x[0]) = uninitialized value from the line:

x[0] = new int[size1];

You will get unpredictable behavior when you use an uninitialized value. 当您使用未初始化的值时,您将获得无法预料的行为。

The second term of the line, **(y + 1) is the worse culprit. 该行的第二项**(y + 1)是最糟糕的元凶。

You allocated memory for y as: 您为y分配的内存为:

y = new int **(&*x);

It's a pointer to a single object of type int** , not an array. 它是一个指向int**类型的对象的指针,而不是一个数组。 y+1 is not a valid pointer. y+1不是有效的指针。 Dereferencing (y+1) leads to undefined behavior. 取消引用(y+1)导致未定义的行为。 In your case, you are seeing exc_bad_access , which makes sense now since you are accessing memory that is out of bounds. 在您的情况下,您会看到exc_bad_access ,这现在exc_bad_access ,因为您正在访问超出范围的内存。

Since I don't know what you are trying to compute in that expression, it's hard for me to suggest something useful. 由于我不知道您要在该表达式中计算什么,因此我很难提出有用的建议。 I hope you have enough to take it from here. 我希望您有足够的机会从这里拿走它。

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

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