假设我有这个。
int ***a = new int**;
*a = new int*;
**a = new int;
***a = 18
所以我们有这样的东西。 [*] -> [*] -> [*]->[18]
是否所有这些指针都在堆栈中并且它们指向的所有内容都在堆中?
最佳答案
假设您的代码内嵌在一个函数中
int ***a = new int**;
a 在栈上。它指向堆上的东西(thing1)
*a = new int*;
a 在栈上。它指向 thing1。 thing1 现在指向堆上的其他东西 (thing2)
**a = new int;
a 在栈上。它指向 thing1。 堆上的 thing1 指向堆上的 thing2。 thing2 指向堆上的一个int
***a = 18
a 在栈上。它指向 thing1。 堆上的 thing1 指向堆上的 thing2。 thing2 指向堆上的一个 int。 那个 int 现在 = 18。
关于c++ - 多级指针,栈还是堆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32358555/