C++ 什么时候对象“超出范围”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10080935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:35:44  来源:igfitidea点击:

When is an object "out of scope"?

c++scopedestructor

提问by Pat Murray

In C++, when is an object defined as "out of scope"?

在 C++ 中,何时将对象定义为“超出范围”?

More specifically, if I had a singly linked list, what would define a single list node object as "out of scope"? Or if an object exists and is being referenced by a variable ptr, is it correct to say that the object is defined as "out of scope" the moment the reference is deleted or points to a different object?

更具体地说,如果我有一个单链表,什么将单个列表节点对象定义为“超出范围”?或者,如果一个对象存在并且被一个变量引用ptr,那么在引用被删除或指向另一个对象时,该对象被定义为“超出范围”是否正确?

UPDATE: Assuming an object is a class that has an implemented destructor. Will the destructor be called the moment the object exits the scope?

更新:假设一个对象是一个具有已实现析构函数的类。对象退出作用域时会调用析构函数吗?

if (myCondition) {
    Node* list_1 = new Node (3);
    Node* list_2 = new Node (4);
    Node* list_3 = new Node (5);

    list_1->next = list_2;
    list_2->next = list_3;
    list_3->next = null;
}

In other words, would the Node being pointed to by list_1call its destructor after this line:

换句话说,是否会list_1在此行之后调用其析构函数来指向被指向的节点:

Node* list_1 = new Node (3);

?

?

回答by sparc_spread

First, remember that objects in C++ can be created either on the stackor on on the heap.

首先,请记住,C++ 中的对象既可以在堆栈上创建,也可以堆上创建。

A stack frame (or scope) is defined by a statement. That can be as big as a function or as small as a flow control block (while/if/foretc.). An arbitrary {}pair enclosing an arbitrary block of code also constitutes a stack frame. Any local variable defined within a frame will go out of scope once the program exits that frame. When a stack variable goes out of scope, its destructor is called.

堆栈帧(或范围)由语句定义。这可以是大如函数或小流量控制块(while/ if/for等)。{}包含任意代码块的任意对也构成堆栈帧。一旦程序退出该框架,在框架内定义的任何局部变量都将超出范围。当堆栈变量超出范围时,将调用其析构函数。

So here is a classic example of a stack frame (an execution of a function) and a local variable declared within it, which will go out of scope once the stack frame exits - once the function finishes:

所以这是一个堆栈帧(一个函数的执行)和一个在其中声明的局部变量的经典示例,一旦堆栈帧退出,它就会超出范围 - 一旦函数完成:

void bigSideEffectGuy () {
    BigHeavyObject b (200);
    b.doSomeBigHeavyStuff();
}
bigSideEffectGuy();
// a BigHeavyObject called b was created during the call, 
// and it went out of scope after the call finished.
// The destructor ~BigHeavyObject() was called when that happened.

Here is an example where we see a stack frame being just the body of an ifstatement:

这是一个示例,我们看到堆栈帧只是if语句的主体:

if (myCondition) {
    Circle c (20);
    c.draw();
}
// c is now out of scope
// The destructor ~Circle() has been called

The only way for a stack-created object to "remain in scope" after the frame is exited is if it is the return value of a function. But that is not really "remaining in scope" because the object is being copied. So the original goes out of scope, but a copy is made. Example:

退出框架后,堆栈创建的对象“保留在范围内”的唯一方法是它是否是函数的返回值。但这并不是真正的“保留在范围内”,因为对象正在被复制。所以原件超出了范围,但制作了一个副本。例子:

Circle myFunc () {
    Circle c (20);
    return c;
}
// The original c went out of scope. 
// But, the object was copied back to another 
// scope (the previous stack frame) as a return value.
// No destructor was called.

Now, an object can also be declared on the heap. For the sake of this discussion, think of the heap as an amorphous blob of memory. Unlike the stack, which automatically allocates and de-allocates the necessary memory as you enter and exit stack frames, you must manually reserve and free heap memory.

现在,也可以在堆上声明一个对象。为便于讨论,可将堆视为无定形的内存块。与在您进入和退出堆栈帧时自动分配和取消分配必要内存的堆栈不同,您必须手动保留和释放堆内存。

An object declared on the heap does, after a fashion, "survive" between stack frames. One could say that an object declared on the heap never goes out of scope, but that's really because the object is never really associated with any scope. Such an object must be created via the newkeyword, and must be referred to by a pointer.

在堆上声明的对象以某种方式在堆栈帧之间“存活”。可以说在堆上声明的对象永远不会超出范围,但这实际上是因为该对象从未真正与任何范围相关联。这样的对象必须通过new关键字创建,并且必须由指针引用。

It is your responsibility to free the heap object once you are done with it. You free heap objects with the deletekeyword. The destructor on a heap object is not called until you free the object.

完成后释放堆对象是您的责任。您可以使用delete关键字释放堆对象。在释放对象之前,不会调用堆对象上的析构函数。

The pointers that refer to heap objects are themselves usually local variables associated with scopes. Once you are done using the heap object, you allow the pointer(s) referring to it to go out of scope. If you haven't explicitly freed the object the pointer is pointing to, then the block of heap memory will never be freed until the process exits (this is called a memory leak).

指向堆对象的指针本身通常是与作用域相关的局部变量。使用完堆对象后,您允许引用它的指针超出范围。如果您没有明确释放指针指向的对象,那么在进程退出之前,堆内存块将永远不会被释放(这称为内存泄漏)。

Think of it all this way: an object created on the stack is like a balloon taped to a chair in a room. When you exit the room, the balloon automatically pops. An object created on the heap is like a balloon on a ribbon, tied to a chair in a room. The ribbon is the pointer. When you exit the room, the ribbon automatically vanishes, but the balloon just floats to the ceiling and takes up space. The proper procedure is to pop the balloon with a pin, and then exit the room, whereupon the ribbon will disappear. But, the good thing about the balloon on the string is you can also untie the ribbon, hold it in your hand, and exit the room and take the balloon with you.

可以这样想:在堆栈上创建的对象就像用胶带粘在房间椅子上的气球。当您离开房间时,气球会自动弹出。在堆上创建的对象就像丝带上的气球,系在房间的椅子上。功能区是指针。当你离开房间时,丝带会自动消失,但气球只是漂浮到天花板上并占据空间。正确的步骤是用别针戳破气球,然后离开房间,然后丝带就会消失。但是,绳子上的气球的好处是你也可以解开丝带,握在手中,然后离开房间并随身携带气球。

So to go to your linked list example: typically, nodes of such a list are declared on the heap, with each node holding a pointer to the next node. All of this is sitting on the heap and never goes out of scope. The only thing that could go out of scope is the pointer that points to the root of the list - the pointer you use to reference into the list in the first place. That can go out of scope.

因此,转到您的链表示例:通常,此类列表的节点在堆上声明,每个节点都持有指向下一个节点的指针。所有这些都在堆上,永远不会超出范围。唯一可能超出范围的是指向列表根的指针 - 您首先用来引用列表的指针。这可能超出范围。

Here's an example of creating stuff on the heap, and the root pointer going out of scope:

这是在堆上创建内容的示例,并且根指针超出范围:

if (myCondition) {
    Node* list_1 = new Node (3);
    Node* list_2 = new Node (4);
    Node* list_3 = new Node (5);

    list_1->next = list_2;
    list_2->next = list_3;
    list_3->next = null;
}
// The list still exists
// However list_1 just went out of scope
// So the list is "marooned" as a memory leak

回答by Tony The Lion

{ //scope is defined by the curly braces
    std::vector<int> vec;
}
// vec is out of scope here!
vec.push_back(15);

回答by Ed S.

When it leaves the scope that it was declared in :)

当它离开它在 :) 中声明的范围时

Your question as it stands is not answerable without seeing the implementation. It comes down to where you declare this node.

如果没有看到实施,您的问题就无法回答。这归结为您声明此节点的位置。

void Foo()
{
    int i = 10;

    {
        int j = 20;
    } // j is out of scope

} // i is out of scope

回答by Kaz

"Out of scope" is a metonymy: as in, using the name or terminology of one concept to talk about something closely related but different.

“超出范围”是一种转喻:例如,使用一个概念的名称或术语来谈论密切相关但又不同的事物。

In C++ a scope is a static region of program text, and so something "out of scope", taken literally, means physically outside of a region of text. For instance, { int x; } int y;: the declaration of yis out of the scope in which xis visible.

在 C++ 中,作用域是程序文本的静态区域,因此“超出作用域”,从字面上看,意味着物理上位于文本区域之外。例如,{ int x; } int y;: 的声明y超出了x可见的范围。

The metonymy "going out of scope" is used to express the idea that the dynamic activation/instantiation of the environment associated with some scope is terminating. And so the variables defined in that scope are going away (thus "out of scope").

转喻“超出范围”用于表达与某个范围相关联的环境的动态激活/实例化正在终止的想法。因此,在该范围内定义的变量将消失(因此“超出范围”)。

What has actually gone "out of scope" is the instruction pointer, so to speak; the program's evaluation is now taking place in a scope which has no visibility to that one. But not everything in a scope goes away! Static variables will still be there the next time the scope is entered.

实际上已经“超出范围”的是指令指针,可以这么说;该计划的评估现在正在一个不可见的范围内进行。但并非范围内的所有内容都会消失!下次进入作用域时,静态变量仍将存在。

"Going out of scope" is not very accurate, but short and everyone understands what it means.

“超出范围”不是很准确,但很简短,每个人都明白它的含义。

回答by DSimon

An object that is declared inside a function (or inside certain curly-brace-bracketed constructs inside functions) falls out of scope when execution leaves that part of code.

当执行离开该部分代码时,在函数内(或在函数内的某些花括号结构内)声明的对象超出范围。

void some_func() {
  std::string x("Hello!");
  // x is in scope here
}
// But as soon as some_func returns, x is out of scope

This only applies to stuff declared on the stack, so it has little to do with singly-linked lists, since list nodes will typically be instantiated on the heap with new.

这仅适用于在堆栈上声明的内容,因此它与单链表关系不大,因为列表节点通常会在堆上使用new.

In this example, the pointerreturned by newwill go out of scope when the function exits, but nothing will happen to the Node itself:

在这个例子中,当函数退出时,返回的指针new将超出范围,但节点本身不会发生任何事情:

void make_a_node() {
  Node* p = new Node;
} // Oh noes a memory leak!