让我们说row = 3
这个例子。
int ***s;
// s=[?]
// s is an uninitialized variable.
s = new int **[row];
// s[*] -> [?]
// [?]
// [?]
// s points to the first element of an array of size 3.
// The elements are uninitialized.
*s = new int *[row];
// s=[*] -> [*] -> [?]
// [?] [?]
// [?] [?]
// We've initialized s[0]. It points to another array of size 3.
// All elements of that array are also uninitialized, along with s[1] and s[2].
**s = new int[row];
// s=[*] -> [*] -> [*] -> [?]
// [?] [?] [?]
// [?] [?] [?]
// More of the same. s[0][0] is initialized.
// This last array contains uninitialized ints, not pointers.
***s = 1;
// s=[*] -> [*] -> [*] -> [1]
// [?] [?] [?]
// [?] [?] [?]
// We traverse three levels of pointers (->) and store 1 in the cell.
所有这些都应该可以编译并正常工作(只要您不访问任何未初始化的元素)。
s + 1
指向第一个数组的第二个元素。
// s=[*] -> [*] -> [*] -> [1]
// s + 1 -> [?] [?] [?]
// [?] [?] [?]
*(s + 1)
指上图中[?]
所指的单元格。s + 1
此单元格未初始化。
**(s + 1)
尝试取消引用无效的垃圾指针(并且经常崩溃)。