How to free memory allocated by a vector of string?

P

Piotr

If I have a vector of string, is a call to 'empty()' sufficient to free
the all memory (i.e. no memory leaks)?

vector <string> str_vector;
str_vector.push_back("hello");
str_vector.push_back("world");

str_vector.empty();

And what if i have a vector of string pointer.
vecto <string*> str_vector;
string* str1= new string();
string* str2= new string();

str_vector.push_back(str1);
str_vector.push_back(str1);

str_vector.empty();
 
S

Sharad Kala

| If I have a vector of string, is a call to 'empty()' sufficient to free
| the all memory (i.e. no memory leaks)?

You don't need to bother about memory management with vector. It takes care
of allocation and deallocation on its own.

| vector <string> str_vector;
| str_vector.push_back("hello");
| str_vector.push_back("world");
|
| str_vector.empty();

Not required.

| And what if i have a vector of string pointer.
| vecto <string*> str_vector;
| string* str1= new string();
| string* str2= new string();

Of course you need to free str1 and str2 on your own. Simple rule is that
you need to delete as many times as you have invoked new.

| str_vector.push_back(str1);
| str_vector.push_back(str1);

Sharad
 
K

Kai-Uwe Bux

Piotr said:
If I have a vector of string, is a call to 'empty()' sufficient to free
the all memory (i.e. no memory leaks)?

vector <string> str_vector;
str_vector.push_back("hello");
str_vector.push_back("world");

str_vector.empty();

This empties the vector and destroys all string objects therein. Upon their
destruction, each of these string objects takes care of its own memory.
There are no leaks.


And what if i have a vector of string pointer.
vecto <string*> str_vector;
string* str1= new string();
string* str2= new string();

str_vector.push_back(str1);
str_vector.push_back(str1);

str_vector.empty();

This will only destroy objects of type string*. It will not destroy the
string objects pointed to by the pointers. Thus, if you do not manually
destroy them elsewhere, you have a leak.


Best

Kai-Uwe Bux
 
G

Greg

Piotr said:
If I have a vector of string, is a call to 'empty()' sufficient to free
the all memory (i.e. no memory leaks)?

vector <string> str_vector;
str_vector.push_back("hello");
str_vector.push_back("world");

str_vector.empty();

And what if i have a vector of string pointer.
vecto <string*> str_vector;
string* str1= new string();
string* str2= new string();

str_vector.push_back(str1);
str_vector.push_back(str1);

str_vector.empty();

std::vector.empty() returns a bool result indicating whether the vector
is in fact empty. It has no effect on the vector or its contents. Call
clear() to erase a std::vector's contents.

Greg
 
R

red floyd

Kai-Uwe Bux said:
This empties the vector and destroys all string objects therein. Upon their
destruction, each of these string objects takes care of its own memory.
There are no leaks.

Sorry, Kai, but as Greg pointed out, it does nothing of the sort. It
checks the vector and returns a bool indicating whether it has any
contents. In this case, it should return false.

std::vector<>::clear(), on the other hand, would do what you describe.
 
K

Kai-Uwe Bux

red said:
Sorry, Kai, but as Greg pointed out, it does nothing of the sort. It
checks the vector and returns a bool indicating whether it has any
contents. In this case, it should return false.

std::vector<>::clear(), on the other hand, would do what you describe.

Thanks for catching that.


Best

Kai-Uwe Bux
 
H

Howard Hinnant

red floyd said:
Sorry, Kai, but as Greg pointed out, it does nothing of the sort. It
checks the vector and returns a bool indicating whether it has any
contents. In this case, it should return false.

std::vector<>::clear(), on the other hand, would do what you describe.

str_vector.capcity() remains non-zero. Each string has returned its
memory, but the vector itself hasn't returned its memory to hold the
strings. If you care about that too you can subsequently:

vector<string>().swap(str_vector);

Indeed, if you're working with a string implemented with sso, and if all
of your strings were short, the call to clear() may not have freed any
memory at all.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top