-4
string *str=new string;

vector<string *> vector;

vector.push_back(str)

in brief, I want know when vector of pointers out of scope, is the pointer's self's occupied space freed?

jiafu
  • 6,338
  • 12
  • 49
  • 73
  • 1
    You cannot push `str` into `vector` because it is a vector of `string`, and `str` is a `string*`. – juanchopanza May 09 '13 at 09:11
  • 1
    That won't compile. If the type contained in a `vector` is a raw pointer the destruction of the `vector` will not result in the `delete`tion of the elements. – hmjd May 09 '13 at 09:12
  • Perhaps you want `std::vector< std::shared_ptr >`? – Branko Dimitrijevic May 09 '13 at 09:14
  • Local variables memory is automatically released when it goes out of scope. However, the memory that might be pointed to by that pointer requires to be explicitly deleted/freed.Also on a side note, did you ask a similar thing [here](http://stackoverflow.com/questions/16457805/after-delete-pointer-the-occupied-space-for-the-str-pointer-self-can-exist)? – Suvarna Pattayil May 09 '13 at 09:22
  • 3
    @jiafu, you keep asking questions like this. Read something on memory management in C++. The core idea is: objects allocated with `new` must be deallocated with `delete`. These are said to have "dynamic storage duration." Global variables have "static storage duration" and exist for the lifetime of the program. Other variables (mainly function local variables, object members) have "automatic storage duration." These are destroyed, and their space reclaimed, when they go out of scope, and only then. – Angew is no longer proud of SO May 09 '13 at 09:29

1 Answers1

4

Your question is a little unclear, but I guess, you want the strings to be allocated dynamically, yet automatically freed after the vector goes out of scope, right?

Vector will destroy only its contents when it is destroyed. That means, when the vector goes out of scope, it will destroy pointers held on its list, but it won't destroy objects pointed to by these pointers. So it may result in memory leak.

Actually, there are a few solutions to this problem. You can store instances of std::string instead of pointers to std::strings:

std::vector<std::string> strings;
strings.push_back("Alice has a cat");

When strings goes out of scope, all strings will be destroyed.

Another option is to use boost (in case you use C++ pre-11) or std (in case you use C++11) class called shared_ptr or unique_ptr. It behaves more less like a pointer, but when it goes out of scope, it destroys the contained object. So if you declare your list like this:

std::vector<std::shared_ptr<std::string>> strings;
std::push_back(std::make_shared<std::string>("Alice has a cat"));

String will be allocated dynamically, and be freed automatically (vector will free all shared_ptr's and if there are no more references to held strings than in these pointers, they will free strings as well).

Read further:

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
Spook
  • 25,318
  • 18
  • 90
  • 167