Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建带指针的三维阵列时出现分段错误(取芯转储)_C++_Arrays_Pointers_Multidimensional Array - Fatal编程技术网

C++ 创建带指针的三维阵列时出现分段错误(取芯转储)

C++ 创建带指针的三维阵列时出现分段错误(取芯转储),c++,arrays,pointers,multidimensional-array,C++,Arrays,Pointers,Multidimensional Array,提前感谢您的帮助 我目前正在通过Alex Allain的“跳入C++”的方法,到目前为止,它是一本很棒的书,就像速成课程中的C++一样。至少对我来说是这样。第14章讨论了如何使用二维数组的指针,其中一个实践问题向读者提出了挑战,读者需要创建一个三维乘法表数组,该数组具有用户在运行时选择的任意长度、宽度和高度。我想我已经把代码准备好并开始工作了,多亏有人在这里,我才能够消除所有与使用指针创建3D数组相关的编译错误 我会贴一张我的问题的照片,但当然我的声誉还不够高。基本上,在我编译并运行程序之后,我

提前感谢您的帮助

我目前正在通过Alex Allain的“跳入C++”的方法,到目前为止,它是一本很棒的书,就像速成课程中的C++一样。至少对我来说是这样。第14章讨论了如何使用二维数组的指针,其中一个实践问题向读者提出了挑战,读者需要创建一个三维乘法表数组,该数组具有用户在运行时选择的任意长度、宽度和高度。我想我已经把代码准备好并开始工作了,多亏有人在这里,我才能够消除所有与使用指针创建3D数组相关的编译错误

我会贴一张我的问题的照片,但当然我的声誉还不够高。基本上,在我编译并运行程序之后,我为3D阵列的高度、宽度和深度输入了3个独立的维度,然后我得到了令人兴奋的消息“分段错误(堆芯转储)”。我的代码中有一些调试cout消息,但没有一条显示出来,这让我感到困惑。我不明白为什么我的代码会在最早出现第一条调试消息之前崩溃。它之所以出现在任何地方,唯一的原因是我为所有三个值都输入了1。但是,只有调试消息显示出来,并且没有任何东西像我想象的那样打印出来应该

这是我的代码:

#include <iostream>

using namespace std;

void printMultTable(int*** p_cube, int height, int width, int depth);

int main() {
    int height = 0;
    int width = 0;
    int depth = 0;

    // Get user input for height, witdth, and depth of multiplication cube
    cout << "Enter height:  ";
    cin >> height;

    cout << "Enter width:  ";
    cin >> width;

    cout << "Enter depth:  ";
    cin >> depth;

    cout << "Made it to point A";

    // Create pointer to pointer for multiplication tables
    int ***p_cube = new int**[depth]; // Creates pointer to 2D pointer array (Layers of Cube)

    cout << "Made it to point B";

    for (int i = 0; i < height; i ++) {
        p_cube[i] = new int*[height]; // Creates pointer to 1D array (Columns of Cube)
        for (int j = 0; i < width; i++) {
            p_cube[i][j] = new int[width]; // Creats pointers that are arrays (Rows of Cube)
        }
    }

    cout << "Made it to point C";


    // Create multiplication table
    for (int i = 0; i < depth; i++) {
        for (int j = 0; j < height; j++) {
            for (int k = 0; k < width; k++) {
                p_cube[i][j][k] = (j+1) * (k+1);
            }
        }
    }

    cout << "Made it to point D";

    // Use printMultTable to print out multiplication table
    printMultTable(p_cube, height, width, depth);

    // Deallocate memory taken by pointers
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++){
            delete[] p_cube[i][j];
        }
        delete p_cube[i];
    }
    delete[] p_cube;
}

void printMultTable(int*** p_cube, int height,int width,int depth) {
    for (int i = 0; i < depth; i++) {
        for (int j = 0; j < height; j++) {
            for (int k = 0; k < width; k++) {
                cout << p_cube[i][j][k] << "\t";
            }
            cout << "\n";
        }
        cout << "\n\n\n";
    }
}
#包括
使用名称空间std;
void printMultTable(整数***立方体,整数高度,整数宽度,整数深度);
int main(){
整数高度=0;
整数宽度=0;
int深度=0;
//获取乘法立方体的高度、宽度和深度的用户输入
cout>高度;
宽度;
深度;
cout此代码:

for (int i = 0; i < height; i ++) {
    p_cube[i] = new int*[height]; // Creates pointer to 1D array (Columns of Cube)
    for (int j = 0; i < width; i++) {
        p_cube[i][j] = new int[width]; // Creats pointers that are arrays (Rows of Cube)
    }
}
for(int i=0;i
应该是:

for (int i = 0; i < depth; i ++) {
    p_cube[i] = new int*[height]; // Creates pointer to 1D array (Columns of Cube)
    for (int j = 0; j < height; j++) {
        p_cube[i][j] = new int[width]; // Creats pointers that are arrays (Rows of Cube)
    }
}
for(int i=0;i
  • 第一个for循环必须从0到
    深度
  • 循环的秒数必须从0到
    高度
  • 您在第二个for循环中比较并增加了
    i
    ,而不是
    j

最明显的问题是,当存在
深度
对象时,您使用
高度
作为第一个循环的范围

int ***p_cube = new int**[depth];
// ...
for (int i = 0; i < height; i ++) {
    p_cube[i] = new int*[height];
    // ...
}

如果这本书的作者提倡裸体分配和不加检查的阅读,他会给你带来很大的伤害!

祝你学习好运

首先,我认为像这样学习int***并不是一个好的开始,因为它非常混乱且效率低下。我建议只分配一块带有width*height*depth元素的内存。要遍历元素,只需从[0..width*height*depth]迭代索引即可。要访问特定元素,可以按如下方式计算:index=i*(宽度*高度)+j*(宽度)+k

但为了回答你的问题,我注意到你在这里犯了一个错误:

int ***p_cube = new int**[depth];
for (int i = 0; i < height; i ++) {
    p_cube[i] = new int*[height];
    ...
int***p_cube=newint**[depth];
对于(int i=0;i

您已经创建了一个大小为“depth”的int**新数组,但随后您对其进行了迭代,就像在其中包含“height”元素一样。正确的做法是对“depth”元素进行迭代,然后分配一个int*数组。

从硬编码3个值开始,去掉>>并查看代码是否有效。然后回来,我个人认为您正在命中一个返回值而不是一个值,或者它看到一个额外的返回值而int无效。您可以将int值从cin转换为字符串,然后在将其转换为int之前检查它是否是有效值,这样您就可以更优雅地失败。这就成功了!您是一个saint ichramm!感谢大家这么快的帮助!这这个解释对我来说最有意义,帮助我澄清了如何构造创建指针数组结构的循环。另外,Dietmar,我必须开始双重检查,以确保我开始检查我的代码,为我的代码提供更大的稳定性和安全性。再次感谢所有的帮助!
int ***p_cube = new int**[depth];
for (int i = 0; i < height; i ++) {
    p_cube[i] = new int*[height];
    ...