简体   繁体   English

什么是基于堆栈的引用?

[英]What is stack-based reference?

What is stack-based references? 什么是基于堆栈的引用? How are they different from references that are members of objects? 它们与对象的引用有何不同? Does the Standard talk about these? 标准会谈这些吗?

I came across this in an article written by Herb Sutter: 我在Herb Sutter撰写的一篇文章中遇到了这个问题:

Q1: Is the following code legal C++? 问题1:以下代码是否合法C ++?

// Example 1

string f() { return "abc"; }

void g() {
const string& s = f();
  cout << s << endl;    // can we still use the "temporary" object?
}

A1: Yes. A1:是的。 This is a C++ feature… the code is valid and does exactly what it appears to do. 这是C ++的功能……代码是有效的,并且确实可以完成它的工作。

Normally, a temporary object lasts only until the end of the full expression in which it appears. 通常,临时对象仅持续到其出现的完整表达式的结尾。 However, C++ deliberately specifies that binding a temporary object to a reference to const on the stack lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error. 但是,C ++故意指定将临时对象绑定到堆栈上const的引用将引用的寿命延长到引用本身的寿命,从而避免了否则会导致常见的悬挂引用错误。 In the example above, the temporary returned by f() lives until the closing curly brace. 在上面的示例中,由f()返回的临时项一直存在,直到右花括号结束为止。 ( Note this only applies to stack-based references. It doesn't work for references that are members of objects. ) 请注意,这仅适用于基于堆栈的引用。不适用于属于对象成员的引用。

In the given context, stack-based references means a reference that is an automatic object on the stack. 在给定的上下文中,基于堆栈的引用是指作为堆栈上自动对象的引用。

That is, in 也就是说,在

// ...
{
  // ...
  const foo& x = some_foo;
  // ...
}
// ...

x is a stack-based object, while the_foo in x是基于堆栈的对象,而the_foo

class bar {
  // ...
  foo& the_foo;
  // ...
};

isn't. 不是。

A stack based reference is a reference that is the parameter of a function call or a local non-static variable inside a block. 基于堆栈的引用是作为函数调用的参数或块内局部非静态变量的引用。 All other references are not stack based. 所有其他引用都不基于堆栈。

int foo;
static int &fooref = foo;  // Not stack based.

class A {
 public:
   A(int &z) : x(z) {}  // z is stack based, x isn't.
   int &x;  // Not stack based.
};

void joe(int &i) { // i is stack based.
   int &k = i;  // k is stack based.
   static int &j = i;  // j is not stack base and this will likely result in a bad error later.
   A a(k);  // a is stack based, but A.x still isn't.
}

A stack-based reference is the alternative to a heap-based reference. 基于堆栈的引用是基于堆的引用的替代方法。 In general, return values and local variable values are allocated in the "call stack", the place that stores what the sequence of currently running functions are. 通常,返回值和局部变量值在“调用堆栈”中分配,该位置存储当前正在运行的函数的顺序。

In general, when you call a function, a 'stack frame' is dropped on the stack, including enough space for all local variables, arguments and the return value of the function. 通常,调用函数时,会在堆栈上放置一个“堆栈框架”,其中包括足够的空间用于所有局部变量,参数和函数的返回值。 While in that function, that stack frame remains alive along with all of its values; 在执行该功能时,该堆栈框架及其所有值仍然有效。 when the function terminates, the stack frame is generally discarded and you return to the next level up. 当函数终止时,通常会放弃堆栈帧,然后返回上一级。

In this case, "abc" goes in the stack frame for f(), but c++ is smart enough to allocate it right next to the stack frame of the parent; 在这种情况下,“ abc”进入了f()的堆栈框架,但是c ++足够聪明,可以将其直接分配给父级的堆栈框架; when f() is popped from the stack, the stack frame for g() (which is directly below f() in the stack, since g() called f()) is adjusted to hang onto the value "abc". 当从堆栈中弹出f()时,将g()的堆栈帧(位于f()的正下方,因为称为f()的g())调整为挂在值“ abc”上。

This describes objects allocated on the stack; 这描述了在堆栈上分配的对象; the alternative is objects in the heap, which are persistent. 替代方法是堆中的对象,这些对象是持久的。 OBjects on the heap are managed using 'new' and 'delete', and held in place by pointers or heap references. 使用“ new”和“ delete”管理堆上的对象,并通过指针或堆引用将其保留在原位。 Stack objects will automatically free when you finish with the function; 完成该功能后,堆栈对象将自动释放。 objects on the heap have to be manually freed. 堆上的对象必须手动释放。

A stack-based reference is simply a reference that lives on the stack. 基于堆栈的引用只是存在于堆栈中的引用。

int main()
{
    int a = 3;
    int &b = a;  // b is a stack-based reference
    ...
}

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

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