通过指针打印二维数组获取垃圾值的原因
Reason for getting garbage values in printing 2-D array through pointer
我有一个指向动态分配的二维数组的指针,我正在尝试通过取消引用该指针来打印数组值。不知何故,一些数组索引显示垃圾值,即使我将它们全部初始化为 1。如果能深入了解我做错了什么,我们将不胜感激。
int main() {
int (*ptr)[4][4];
int** array;
array = new int*[4];
for (int i = 0; i < 4; i++)
{
array[i] = new int[4];
}
ptr = (int(*)[4][4])(*array);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
array[i][j] = 1;
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; ++j)
{
cout<<"Val "<<*ptr[i][j]<<" And index "<<i<<" and "<<j<<endl;
}
}
return 0; }
我的输出:
Val 1 And index 0 and 0
Val 0 And index 0 and 1
Val 1 And index 0 and 2
Val 0 And index 0 and 3
Val 1 And index 1 and 0
Val 0 And index 1 and 1
Val 1 And index 1 and 2
Val 0 And index 1 and 3
Val 543973718 And index 2 and 0
Val 1852383332 And index 2 and 1
Val 10 And index 2 and 2
Val 0 And index 2 and 3
Val 0 And index 3 and 0
Val 0 And index 3 and 1
Val 0 And index 3 and 2
Val 0 And index 3 and 3
让我们看看你的基本代码。
int (*ptr)[4][4];
int** array;
array = new int*[4];
for (int i = 0; i < 4; i++)
{
array[i] = new int[4];
}
ptr = (int(*)[4][4])(*array);
这是重要的部分。让我们看看数组是什么。
Array
是指向指针的指针。当您以非常 C 风格的方式执行此操作时,您所做的是好的。您正在为 4 个指针分配 space,然后每个指针都为 4 个整数分配 space。所以这给了你一个 4x4 数组(有点)。
但是 ptr
是什么?
int (*ptr)[4][4];
Ptr 是指向 4x4 整数数组的指针。那不是数组。数组是指向 4 个指针的指针,每个指针都指向 space 4 个整数。他们不一样。
它们的存储方式不同。 Array
总共有 5 个指针:本身加上 4 个新指针。
ptr
期望一个指针(本身)指向内存中连续的 16 个整数,没有其他点。
这就是为什么它没有按您预期的那样工作。
你也把它弄得太复杂了,但也许是有原因的。 C++ 的方法是大幅减少 new/delete 的数量。您可以使用 STL 中的容器。
或者您也可以这样做:
int array[4][4];
不涉及指针。但也许你希望它可变是有原因的。
我有一个指向动态分配的二维数组的指针,我正在尝试通过取消引用该指针来打印数组值。不知何故,一些数组索引显示垃圾值,即使我将它们全部初始化为 1。如果能深入了解我做错了什么,我们将不胜感激。
int main() {
int (*ptr)[4][4];
int** array;
array = new int*[4];
for (int i = 0; i < 4; i++)
{
array[i] = new int[4];
}
ptr = (int(*)[4][4])(*array);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
array[i][j] = 1;
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; ++j)
{
cout<<"Val "<<*ptr[i][j]<<" And index "<<i<<" and "<<j<<endl;
}
}
return 0; }
我的输出:
Val 1 And index 0 and 0
Val 0 And index 0 and 1
Val 1 And index 0 and 2
Val 0 And index 0 and 3
Val 1 And index 1 and 0
Val 0 And index 1 and 1
Val 1 And index 1 and 2
Val 0 And index 1 and 3
Val 543973718 And index 2 and 0
Val 1852383332 And index 2 and 1
Val 10 And index 2 and 2
Val 0 And index 2 and 3
Val 0 And index 3 and 0
Val 0 And index 3 and 1
Val 0 And index 3 and 2
Val 0 And index 3 and 3
让我们看看你的基本代码。
int (*ptr)[4][4];
int** array;
array = new int*[4];
for (int i = 0; i < 4; i++)
{
array[i] = new int[4];
}
ptr = (int(*)[4][4])(*array);
这是重要的部分。让我们看看数组是什么。
Array
是指向指针的指针。当您以非常 C 风格的方式执行此操作时,您所做的是好的。您正在为 4 个指针分配 space,然后每个指针都为 4 个整数分配 space。所以这给了你一个 4x4 数组(有点)。
但是 ptr
是什么?
int (*ptr)[4][4];
Ptr 是指向 4x4 整数数组的指针。那不是数组。数组是指向 4 个指针的指针,每个指针都指向 space 4 个整数。他们不一样。
它们的存储方式不同。 Array
总共有 5 个指针:本身加上 4 个新指针。
ptr
期望一个指针(本身)指向内存中连续的 16 个整数,没有其他点。
这就是为什么它没有按您预期的那样工作。
你也把它弄得太复杂了,但也许是有原因的。 C++ 的方法是大幅减少 new/delete 的数量。您可以使用 STL 中的容器。
或者您也可以这样做:
int array[4][4];
不涉及指针。但也许你希望它可变是有原因的。