From: jamaj on
Dear All,

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?

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?

Thanks in advance.

Best Regards,


Jos� Augusto Jr.






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

From: Ulrich Eckhardt on
jamaj 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?

All[1] local objects are destroyed at the end of the function. This involves
e.g. calling a destructor, which in this case will deallocate all elements
of the vector.

> 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?

Yes, because you can not pass it to a function. If you pass it to a
function, the function will actually receive a copy. Similarly if you
return it from a function, the calling code will receive a copy.

Uli

[1] The only exception is local objects declared as 'static', but I'll
ignore this case.


--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


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

From: Onk on
On May 21, 1:03 pm, jamaj <jama...(a)gmail.com> wrote:
> Dear All,
>
> 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, vectors (and other STL containers like map, set) automatically
cleanup the memory when their class object goes out of scope.

> 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?
>
If you want to store the vector and make it accessible even after
foo() execution is complete, you will need to either pass the vector
by value or allocate the entire vector on heap like this:
std::vector<std::string> *v = new std::vector<std::string>

{ quoted signature and banner removed; please do it yourself. -mod }

Thanks,
Onkar


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

From: Saeed Amrollahi on
On May 21, 12:03 pm, jamaj <jama...(a)gmail.com> wrote:
> Dear All,
>
> 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?
>
> 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?

{ quoted signature and banner removed; please do it yourself. -mod }

Hi

The fundamental rule in C++:
The constructor of an object is called at the point of definition
and
the destructor of that object is called at the end of scope.
Your solution is compatible with C spirit. If you can change the
signature/interface of foo, there are good and better ways:

1. Good way:
void foo(vector<string>& s)
{
// work on S
}

// ...
vector<string> SS;
foo(SS); // call with SS

2. Better way:
vector<string> foo()
{
vector<string> SS;
// ...
return SS; // return a copy of SS
}

Regards,
-- Saeed Amrollahi


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

From: Goran on
On May 21, 10:03 am, jamaj <jama...(a)gmail.com> wrote:
> Dear All,
>
> 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?
>
> 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?

Your question has strictly nothing to do with std::vector, and
strictly everything with object lifetime in C (not even C++!) language
(replace "object" with "variable" if you think there's no objects in
C).

In C language, if you do this:

typedef struct vector {int i;};
static vector* pv;
void f1()
{
vector v;
pv = &v;
}

f2()
{
pv->i = 10; // Undefined behavior
}

You are invoking undefined behavior. This is because, at the end of
f1, local (a.k.a. automatic) variable v has ceased to exist (v exists
from the moment of it's declaration to the end of the function). But
you assigned it's address to pv, and pv, who is a static variable,
does exist. But once f1 is finished, pv does not point to an existing
variable. It points to memory that does not belong to your program.
Accessing that memory invokes undefined behavior.

I suggest you to get a C book and learn about storage classes
(notably, automatic, static, heap). Please don't be offended, but
given your question, I think that you don't understand C language
well, and in my opinion, if that's the case, you should lay off C++
and get a better understanding of C first. In my opinion, a rather
solid grasp of C is an absolute prerogative to understanding what-s
and why-s of C++.

Goran.


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