迷失在指针的世界里
Lost in a world of pointers
请帮助我,我无法理解这段代码是如何工作的。 我似乎无法在脑海中想象它。一个详尽的解释可以省去我将来在生活中失败的麻烦。谢谢。
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_ptr
和std::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::vector
或std::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:...
希望它有帮助
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 激励'inline'说明符的真实世界示例?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 使用Unreal C++获取VR耳机的世界位置/方向
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 迷失在指针的世界里
- 迷失在指针世界中,通过数组写入变量