开发者

C++ Memory allocation question involving vectors

开发者 https://www.devze.com 2022-12-29 19:30 出处:网络
vector< int > vect; int *int_ptr = new int(10); vect.push_back( *int_ptr ); I under stand that every \"new\" needs to be followed by a \"delete\" at some point but does the clear() method clea
vector< int > vect;
int *int_ptr = new int(10);
vect.push_back( *int_ptr );

I under stand that every "new" needs to be followed by a "delete" at some point but does the clear() method clean this memory?

What about this meth开发者_如何学运维od of doing the same thing:

vector< int > vect;
int int_var = 10;
vect.push_back( int_var );

From what I understand, clear() calls the variables destructors, but both vect.push_back() methods in this example push an object on the vector, not a pointer. so does the first example using an int pointer need something other than clear() to clean up memory?


The first method leaks because the vector never takes ownership of the allocated pointer. In fact, it doesn't contain a pointer at all, only a copy of the value.

The second method does not leak, as no memory is dynamically allocated (except internally in the vector -- it will handle that memory itself).


When you push_back on a vector, you add a copy of the data to the vector. Therefore, in both cases, the original data still needs to be freed. In the first case, you need to delete it; in the second, it will be "freed" by the stack pointer as it goes out of scope.


Vectors make copies on push_back. Since a pointer is 'just another variable' (but one that happens to point to memory), when you push_back an integer pointer that has been previously allocated, you copy the pointer's value into the vector, causing a potential dangling pointer, since there will be two pointers pointing at the same spot in memory.

In your first example, you would need to delete the memory manually. One strategy I've used in the past for meddling with graph classes is to have something like this (vast amounts of stuff redacted due to being at work and typing quickly):

class graph //quick-format
{
vector<node*> nodes;
add_node(node n)
{
  node *temp = new node; 
  *temp = n; 
  nodes.push_back(temp) 
}
~graph()
{ 
   for(int i = 0; i < nodes.size(); i++) 
      delete nodes[i]; 
}
};

As a caveat, graph's copy semantics will have to be examined. As it stands, it will result in deleting previously-free'd memory. The advantage is that you can always have the same set of nodes hanging around. Caveat Emptor, like any direct memory usage..

However, if you simply push a non-pointer variable, there is no possibility of memory leaking from your end. Possibly the vector will leak, but... that is practically impossible at this point in the maturity of the tools.

0

精彩评论

暂无评论...
验证码 换一张
取 消