简体   繁体   English

多级指针,堆栈还是堆?

[英]Multilevel pointers, stack or heap?

Lets say I have this.可以说我有这个。

int ***a = new int**;
*a = new int*;
**a = new int;
***a = 18

So we have something like this.所以我们有这样的事情。 [*] -> [*] -> [*]->[18]

Are all of these pointers in the stack and everything they are pointing to in the heap?所有这些指针都在堆栈中吗?它们指向的所有东西都在堆中吗?

assuming your code is inline in a function假设您的代码内联在函数中

int ***a = new int**;

a is on the stack. a 在堆栈上。 It points to something (thing1) on the heap它指向堆上的某个东西 (thing1)

*a = new int*;

a is on the stack. a 在堆栈上。 It points to thing1.它指向thing1。 thing1 now points to something else on the heap (thing2) thing1 现在指向堆上的其他东西(thing2)

**a = new int;

a is on the stack. a 在堆栈上。 It points thing1.它指出thing1。 thing1 on the heap points to thing2 on the heap.堆上的thing1指向堆上的thing2。 thing2 points to an int on the heap thing2 指向堆上的一个 int

***a = 18

a is on the stack. a 在堆栈上。 It points thing1.它指出thing1。 thing1 on the heap points to thing2 on the heap.堆上的thing1指向堆上的thing2。 thing2 points to an int on the heap. thing2 指向堆上的一个 int。 that int now = 18.现在 int = 18。

The very first pointer is on stack.第一个指针在堆栈上。 The rest of them is on heap, since they were allocated using new.其余的在堆上,因为它们是使用 new 分配的。

a is on the stack. a在堆栈上。 The rest, created by new is on the heap其余的,由new创建在堆上

To understand what the code does, the concept of the stack is unnecessary.要理解代码的作用,堆栈的概念是不必要的。 Some implementation that you use might put a on the stack, but that doesn't change the meaning of the code, and this detail is not necessary to understand what the code does.您使用的某些实现可能会将a放在堆栈上,但这不会改变代码的含义,并且此细节对于理解代码的作用不是必需的。

If you don't believe me: in the C++ standard, the word stack only refers to std::stack and to the abstract stack unwinding concept.如果您不相信我:在 C++ 标准中,堆栈一词仅指std::stack和抽象堆栈展开概念。

To be specific, we must assume how your code is used.具体来说,我们必须假设您的代码是如何使用的。 Let's assume that it is in a block of its own:让我们假设它在它自己的一个块中:

{
  int ***a = new int**; 
  *a = new int*;        
  **a = new int;
  ***a = 18;
}

What we are concerned with then is what is the storage duration of various pieces of storage.那么我们关心的是各个存储块的存储时长是多少。

  1. The a has automatic storage duration. a具有自动存储持续时间。 The storage exists until the closing brace.存储存在直到右括号。

  2. a points to storage of type int** of dynamic storage duration. a指向动态存储持续时间的int**类型的存储。 The storage lasts until you explicitly deallocate it - in this case, it outlives a .存储持续,直到明确释放它-在这种情况下,它会超越a

  3. *a points to storage of type int* of dynamic storage duration. *a指向动态存储持续时间的int*类型的存储。 Here too, the storage lasts until you explicitly deallocate it - in this case, it outlives a .在此,存储持续,直到明确释放它-在这种情况下,它会超越a

  4. **a points to storage of type int of dynamic storage duration. **a指向动态存储持续时间的int类型存储。 Here too, the storage lasts until you explicitly deallocate it - in this case, it outlives a .在此,存储持续,直到明确释放它-在这种情况下,它会超越a

  5. ***a is the value int(18) stored in a piece of dynamic storage that **a points to. ***a是存储在**a指向的动态存储器中的值int(18)

What the program does, and how it behaves, has to do with these storage durations.程序的作用以及它的行为方式与这些存储持续时间有关。 It has nothing to do with whether a is on some stack.它与a是否在某个堆栈上无关。 For all you know, you've compiled this code using emscripten , and the value lives in a javascript variable.就您所知,您已经使用emscripten编译了这段代码,并且该值存在于一个 javascript 变量中。

What is important that the storage duration of a is automatic, and its extent is to the end of the block in this case.什么是重要的的存储持续时间a是自动的,并且其范围是在这种情况下,块的结尾。 The storage duration of everything you allocated with new is dynamic, and fully under your control.您使用new分配的所有内容的存储期限是动态的,并且完全在您的控制之下。

With the code shown, there's no long-term way to access the three pieces of storage with dynamic storage duration: they will outlive a 's storage duration and turn into memory leaks as soon as the block ends.使用显示的代码,没有长期的方法来访问具有动态存储持续时间的三块存储:它们将超过a的存储持续时间在块结束后立即变成内存泄漏。 If you pass a out of the block, you'll be able to prevent this and retain the way of accessing all three dynamic storage duration pieces of storage.如果你传递a出块的,你就可以防止这种情况,并保留访问存储的所有三个动态存储时间段的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM