C ++参数堆与堆栈 [英] C++ Arguments Heap vs Stack

查看:100
本文介绍了C ++参数堆与堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建两个向量,一个在堆上,一个在栈上:

Suppose I create two vectors, one on the heap and one on the stack:

Vector<int> vector1;
Vector<int>* vector2 = new Vector<int>;

然后我将vector1传递给两个函数,例如foo1(Vector<int>)foo2(Vector<int>&).我还将vector2传递给foo3(Vector<int>*).

I then pass vector1 into two functions, say, foo1(Vector<int>) and foo2(Vector<int>&). I also pass vector2 into foo3(Vector<int>*).

由于我是C ++的新手,所以我对这里的行为差异感到困惑.

Since I'm new to C++, I'm rather confused by the difference in behaviours here.

  1. 我是说对foo1,整个vector1被复制,对于foo2,仅对vector1的引用传递到函数中吗?

  1. Am I right to say that for foo1, the entire vector1 gets copied, and for foo2, only the reference to vector1 gets passed into the function?

但是不是在堆栈上声明的vector1,应该在创建它的范围之外的其他任何地方(即,在foo2内部)不可访问吗?

But isn't vector1, declared on the stack, supposed to be inaccessible anywhere else (i.e. from inside foo2) except the scope in which it was created?

此外,修改foo1foo2vector1的内容是否会影响原始矢量?

Also, does modifying the contents of vector1 inside foo1, foo2 affect the original vector?

推荐答案

我是说对foo1来说,整个vector1被复制,对于foo2来说,只有对vector1的引用才传递给函数吗?

Am I right to say that for foo1, the entire vector1 gets copied, and for foo2, only the reference to vector1 gets passed into the function?

正确.

但是不是在堆栈上声明的vector1,除了创建它的作用域之外,在其他任何地方(即从foo2内部)都不可访问吗?

But isn't vector1, declared on the stack, supposed to be inaccessible anywhere else (i.e. from inside foo2) except the scope in which it was created?

这只是vector1名称,无法在外部访问,但是您可以自由传递其地址(通过引用或指针传递).当然,只要函数不返回,它就一直存在,因此返回指向它的指针将是一个错误(因为它将指向不再存在的对象).

It's just the name of vector1 that is not accessible outside, but you can freely pass its address around (passing it by reference or by pointer). Of course, it continues to exist as long as the function does not return, so returning a pointer to it would be an error (because it would point to a no longer existing object).

这与堆上的分配不同,后者没有作用域绑定的生存期或自动删除-留给您.这是一把双刃剑:您可以具有自定义生命周期的对象,但是您必须记住在不再需要它们时实际上delete执行它们.

This differs from allocation on the heap, that has no scope-bound lifetime or automatic deletion - it's left to you. This is double-edged sword: you can have objects with custom lifetime, but you must remember do actually delete them when they are no longer needed.

此外,修改foo1foo2vector1的内容是否会影响原始矢量?

Also, does modifying the contents of vector1 inside foo1, foo2 affect the original vector?

foo1的参数是一个新的,分离的向量,对其所做的任何修改都将保留在该函数的本地.相反,foo2的参数引用了vector1,因此它将影响原始向量(实际上,对其他对象的引用通常被描述为别名".)

The argument to foo1 is a new, separated vector, any modification done to it will remain local to the function. The argument to foo2, instead, refers to vector1, so it will affect the original vector (actually, references are often described as "aliases" for other objects).

vector1是否会在其作用域结束时自动销毁,还是我们必须手动将其删除?

And is vector1 automatically destroyed at the end of its scope, or do we have to delete it manually?

它会像任何局部变量一样被自动销毁. 但是:vector2将必须手动删除,因为本地对象只是 pointer ,没有析构函数(默认情况下,它不拥有该对象)指向).

It's automatically destroyed, as any local variable. But: vector2 will have to be deleted manually, since the local object is just the pointer, that has no destructor (by default it doesn't own the object it points to).

这篇关于C ++参数堆与堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆