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 entire vector1
gets copied, and for foo2
, only the reference to vector1
gets passed into the function?
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?
Also, does modifying the contents of vector1
inside foo1
, foo2
affect the original vector?
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).
If you want to pass a vector
by reference, then you do it as shown in pseudocode for foo2()
.
vector<int> vector1
will allocate the vector, i.e. the header info, on the stack, but the elements on the free store ("heap").
vector<int> *vect = new vector<int>;
allocates everything on the free store.
vector<int*> vect;
will allocate the vector
on the stack and a bunch of pointers on the free store.
Source: https://stackoverflow.com/a/8036528/2591612
As far as a copy passed to the function in foo1
, the changes will remain local to the function. The vector is automatically destroyed at the end of its scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With