迷失在指针的世界里

Lost in a world of pointers

本文关键字:世界 指针 迷失      更新时间:2023-10-16

请帮助我,我无法理解这段代码是如何工作的。 我似乎无法在脑海中想象它。一个详尽的解释可以省去我将来在生活中失败的麻烦。谢谢。

int **data = new int*[row];
for(i=0;i<row;i++)
data[i] = new int[col];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
data[i][j] = rand() % u;
}
}
#include <cstddef>  // std::size_t
#include <cstdlib>  // std::srand(), std::rand()
#include <ctime>    // std::time()
int main()
{
// Initialize the random number generator with the current time.
// If you don't, std::rand() behaves as if std::srand(1) had been called.
std::srand(static_cast<unsigned>(time(nullptr)));
std::size_t rows =  5;
std::size_t cols = 10;
int u = 100;
int **data =  // data is a pointer to a pointer to int
new int*[rows];  // allocates an array of type int* with size of row
// and return a pointer to it
for (std::size_t i = 0; i < rows; i++)
data[i] =  // every pointer in that array is assigned
new int[cols];  // a pointer to an array of type int with size col
for (std::size_t i = 0; i < rows; i++)  // walk through the (int**) in data
{
for (std::size_t j = 0; j < cols; j++)  // walk through the (int*) in data[i]
{
data[i][j] = std::rand() % u;  // access the j th int in data[i]
}
}
// free the memory, when you are done using it *):
for (std::size_t i = 0; i < rows; i++)
delete[] data[i];
delete[] data;
}

*(更好:使用智能指针(如std::unique_ptrstd::shared_ptr(,这样您就不必关心资源管理:

#include <memory>
// ...
auto data = std::make_unique<std::unique_ptr<int[]>[]>(rows);
for (std::size_t i = 0; i < rows; i++)
data[i] = std::make_unique<int[]>(cols);

更好的是:使用像std::vectorstd::deque这样的容器。

// "int **data": declare a variable that store address of anthor address storing variables i.e. declare a pointer(*) to anther pointer(*)
// [addr of *data](data)-->[addr of **data](*data)-->[actual value](**data)
// "int **data = new int*[row]": initialize data with address of the first
// element(which is again a pointer) of the array of length 'row' and int*
// indicates that this array is an array of integer pointers(i.e. each element in this array stores address of another 1-D array containing integers.)
int **data = new int*[row];
// Now traverse through each element of the array whos address is stored
// in the data variable i.e. if you do printf(data), it will print out the
// the address of the first element of this array pointed by data. While traversing, initialize each element of this array with addresses of new array(hence data points to array and each element in this array points to another array forming a 2-D array or matrix)
for(i=0;i<row;i++)
data[i] = new int[col];
// Rest should be obvious.
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
data[i][j] = rand() % u;
}
}
// Feel free to ask questions if this still does not make sence.
// Also my C knowledge is a bit rusty so corrections are always welcome.

指针本质上是一个变量,其中包含对象的内存地址。在您的案例中,对象的类型是"int"。

memory:
00000000: [some value] -- start
...
pointer : [address1] pointer to int (int*) +
...                                        |
address1: [value of int] <-----------------+

现在你拥有的是int**,它是一个指向 int 的指针。

memory:
00000000: [some value] -- start
...
pointer : [address1] pointer to int (int**) ----+
...                                             |
address1 : [address2] pointer to int (int*) + <-+
...                                         |
address2: [value of int] <------------------+

现在,你做了什么,你分配了一个指向大小行的 int 的指针数组

memory:
00000000: [some value] -- start
...
pointer : [address1] pointer to int (int**) -----+
...                                              |
address1 : [address-a] pointer to int (int*) + <-+
address2 : [address-b] pointer to int (int*) 
address3 : [address-c] pointer to int (int*) 
address4 : [address-d] pointer to int (int*) 
address5 : [address-e] pointer to int (int*) +
...                                          |
address-e: [value of int] <------------------+
...
address-b:...

希望它有帮助