C ++参数堆与堆栈 [英] C++ Arguments Heap vs Stack
问题描述
假设我创建两个向量,一个在堆上,一个在栈上:
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.
-
我是说对
foo1
,整个vector1
被复制,对于foo2
,仅对vector1
的引用传递到函数中吗?
Am I right to say that for
foo1
, the entirevector1
gets copied, and forfoo2
, only the reference tovector1
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?
此外,修改foo1
,foo2
中vector1
的内容是否会影响原始矢量?
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 entirevector1
gets copied, and forfoo2
, only the reference tovector1
gets passed into the function?
正确.
但是不是在堆栈上声明的
vector1
,除了创建它的作用域之外,在其他任何地方(即从foo2
内部)都不可访问吗?
But isn't
vector1
, declared on the stack, supposed to be inaccessible anywhere else (i.e. from insidefoo2
) 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.
此外,修改
foo1
,foo2
中vector1
的内容是否会影响原始矢量?
Also, does modifying the contents of
vector1
insidefoo1
,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屋!