C++ 参数堆与堆栈 [英] C++ Arguments Heap vs Stack
问题描述
假设我创建了两个向量,一个在堆上,一个在堆栈上:
Vector向量1;Vector* vector2 = new Vector;
然后我将 vector1
传递给两个函数,例如 foo1(Vector
和 foo2(Vector
.我还将 vector2
传入 foo3(Vector
.
由于我是 C++ 的新手,我对这里的行为差异感到相当困惑.
我的说法是否正确,对于
foo1
,整个vector1
都会被复制,而对于foo2
,只有对vector1
被传递到函数中了吗?但是在堆栈上声明的
vector1
不是应该在其他任何地方都不可访问(即从foo2
内部),除了它所在的范围创建?另外,修改
foo1
、foo2
里面vector1
的内容会影响原来的vector吗?vector1
是在其作用域结束时自动销毁,还是必须手动删除?
我的说法是否正确,对于
foo1
,整个vector1
都被复制,而对于foo2
,只有对的引用vector1
被传递到函数中?
正确.
<块引用>但是在堆栈上声明的 vector1
不是应该在除了创建它的作用域之外的其他任何地方(即从 foo2
内部)都无法访问吗?
它只是vector1
的name 是外部无法访问的,但是您可以自由地传递它的地址(通过引用或通过指针传递它).当然,只要函数没有返回,它就会一直存在,所以返回一个指向它的指针将是一个错误(因为它会指向一个不再存在的对象).
这与堆上的分配不同,堆上没有范围限制的生命周期或自动删除 - 由您决定.这是一把双刃剑:您可以拥有具有自定义生命周期的对象,但您必须记住在不再需要它们时实际删除
它们.
另外,修改foo1
、foo2
里面的vector1
的内容对原始vector有影响吗?
foo1
的参数是一个新的、分离的向量,对它所做的任何修改都将保留在函数的本地.foo2
的参数反而引用了 vector1
,所以它会影响原始向量(实际上,引用通常被描述为其他对象的别名").><块引用>
vector1
是在其作用域结束时自动销毁,还是必须手动删除?
它会像任何局部变量一样自动销毁.但是:vector2
必须手动删除,因为本地对象只是指针,没有析构函数(默认情况下它没有)t 拥有它指向的对象).
Suppose I create two vectors, one on the heap and one on the stack:
Vector<int> vector1;
Vector<int>* vector2 = new 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>*)
.
Since I'm new to C++, I'm rather confused by the difference in behaviours here.
Am I right to say that for
foo1
, the entirevector1
gets copied, and forfoo2
, only the reference tovector1
gets passed into the function?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?Also, does modifying the contents of
vector1
insidefoo1
,foo2
affect the original vector?- And is
vector1
automatically destroyed at the end of its scope, or do we have to delete it manually?
Am I right to say that for
foo1
, the entirevector1
gets copied, and forfoo2
, only the reference tovector1
gets passed into the function?
Correct.
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?
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).
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.
Also, does modifying the contents of
vector1
insidefoo1
,foo2
affect the original vector?
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).
And is
vector1
automatically destroyed at the end of its scope, or do we have to delete it manually?
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屋!