From: Daniel T. on
jamaj <jamajbr(a)gmail.com> wrote:

> I'm learning STL vectors. So, excuse me for the dummy question.
>
> Suppose that, in some function, I create a
>
> foo()
> {
> vector<string> SS;
> }
>
> Is this vector deallocated at the end of the foo() function?

Yes.

> If I want to pass it to some other function that will store it
> somewhere, do I need to use a vector * an allocate it someway and pass
> the pointer to the vector, instead the vector itself? Does it make any
> sense? If so, how do I do it?

It depends on how that function will be storing it. Some examples:

vector<string> storage;

void fn(const vector<string>& in) {
storage = in;
}

int main() {
vector<string> ss;
fn(ss);
}

With the above, 'storage' and 'ss' end up with the same contents but
they are copies of each other. This is usually how it's done, especially
if "storage" is actually a member-variable of some class. (A copy is
made to support encapsulation.)

If you don't want to copy, then you need to use a pointer:

vector<string>* storage;

void fn(vector<string>* in) {
storage = in;
}

int main() {
vector<string>* v = new vector<string>;
fn(v);

delete v; // you have to manually delete the vector.
}

Of course there are several different smart pointers that you can use to
make sure the deletion is done properly and automatically for you.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]