Why is this (pointers)?

Status
Not open for further replies.

lobobo

Ars Scholae Palatinae
778
Hello all,

I'm wondering if someone can explain "why this is" in C++.

Let's say there are two deques.
Code:
deque<pair<int, int>*> PointersToPairs;
deque<pair<int, int>*> CopyOfPointersToPairs;

Let's say we use the new operator and a simple loop to add five elements to the first deque.

Code:
for(short N = 0; N < 5; N++)
PointersToPairs.push_back(new pair<int, int>(1, 1));

Now let's say the deque marked copy above is given a copy of the all of the addresses contained in each element of the first deque.

Code:
CopyOfPointersToPairs = PointersToPairs;

Using a simple loop to output the contents in each element of the deque I see that the contents are the same. The content in each element is the address to the pair we declared using the new operator. I am not trying to get the value of the first or second component of the pair, just the address to the pair.

So at this point I'm operating under the assumption that there are two deques, each filled with pointers to the same single set of five pairs in memory.

If I call the .clear() method on one of those deques, shouldn't the pointers in the other deque dangle? In other words, .clear() clears the elements in the deque and for each pointer in the deque it deletes the content in memory pointed to by the pointer.

But I can still access those pairs from the other deque it appears they still exist in memory. Am I wrong? Does it not delete those pairs from memory? Cplusplus states that each element is cleared and its destructor called, which I assumed was happening because delete was being called.

Note: If I iterate through the deque and call delete on each element, the outcome is as I expected the above to be. Each element now points to a pair that doesn't exist.

So is it safe to say that my assumption was wrong? That calling the .clear() method on a deque doesn't call delete on its elements? I assumed calling the destructor implied that delete was being called on it...
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Whose STL are you using? SGI, for one, only talks about iterator invalidation; nothing about calling Delete on child elements. The MSVC docs are similar.

I would be surprised if an STL implementation actually worked the way you describe. Chances are the Cplusplus docs are making a vague statement about the difference between clear/erase and resize -- the latter might not allocate/free the container's internal memory buffers immediately.
 

allotrope

Smack-Fu Master, in training
66
To split a hair the five pairs aren't dangling yet, because the elements of your other deque still point to the instances.

But once you call CopyOfPointersToPairs.clear() (either explicitly or via deque::~deque), the instances will be dangling, because the item type is pair<int,int>* which is a raw pointer type and has no destructor. So they do end up leaking.

If you declared your collections as deque<shared_ptr<pair<int,int>>> then you'd have a collection of shared_ptr<pair<int,int>>. Then both PointersToPairs.clear() and CopyOfPointersToPairs.clear() would call each shared_ptr instance's destructor. Each call to shared_ptr::~shared_ptr checks its (shared) reference counter and delete's the pointed-to pair<int,int> instance upon the reference counter reaching zero. So no leak.
 
Richard Berg":x9x54rq0 said:
Whose STL are you using? SGI, for one, only talks about iterator invalidation; nothing about calling Delete on child elements. The MSVC docs are similar.

I would be surprised if an STL implementation actually worked the way you describe. Chances are the Cplusplus docs are making a vague statement about the difference between clear/erase and resize -- the latter might not allocate/free the container's internal memory buffers immediately.
It would be completely against the rules to delete the pointers.
 

lobobo

Ars Scholae Palatinae
778
To split a hair the five pairs aren't dangling yet, because the elements of your other deque still point to the instances.
But I thought the whole issue with dangling pointers was because it did not matter if another pointer was pointing to something that went out of scope or for whatever reason no longer exists. In other words the default "dumb" pointers will point to something even if it goes out of scope so if you have two pointers pointing to the same thing and the "thing" goes out of scope or no longer exists and only one of the pointers are updated or set to NULL, accessing the other pointer will lead to death. :(

To illustrate what I am saying: If those five pairs aren't dangling yet because some other deque has pointers to those five pairs, why are they dangling if I iterate through one of the deques and call delete on each element?

Surely the other deque still has pointers to the pairs but they now no longer point to valid pairs because I called delete on them.

pointers have no destructor.
Yes but what about pointers pointing to objects that do have destructors? I assumed that when it states that each element would have its destructor called it meant that it would call the destructor through the pointer.

Unless it quite literally means it would only call the destructor, so if its a pointer it will not call the destructor of the object pointed to by the pointer.

Whose STL are you using?
The C++ STL.
 

allotrope

Smack-Fu Master, in training
66
lobobo":eeyv7fr3 said:
Surely the other deque still has pointers to the pairs but they now no longer point to valid pairs because I called delete on them.
I'm sorry, I missed the explicit delete's at the end of your post. :rolleyes: at me. Yes they're dangling after that.

pointers have no destructor.
Yes but what about pointers pointing to objects that do have destructors? I assumed that when it states that each element would have its destructor called it meant that it would call the destructor through the pointer.

Unless it quite literally means it would only call the destructor, so if its a pointer it will not call the destructor of the object pointed to by the pointer.
That's exactly what it means. A pointer has no destructor to call.
 
Yes but what about pointers pointing to objects that do have destructors?
They're still just pointers. They still have no destructor.

I assumed that when it states that each element would have its destructor called it meant that it would call the destructor through the pointer.
No. It means literally what it says.

Unless it quite literally means it would only call the destructor, so if its a pointer it will not call the destructor of the object pointed to by the pointer.
Yes. Which is why it says what it does.
 

Arakageeta

Ars Scholae Palatinae
729
Subscriptor++
In C++, in general, any object/memory allocated by new (or new[]) must be freed with delete (or delete[]) when the last pointer to that object/memory loses scope. Otherwise, you'll have a memory leak. As DrPizza has been saying, deque::clear() does not invoke 'delete' on the contained pointers (though it will invoke 'delete' on the _buffers_ it is using to store your pointers...).

C++ is not like Java in that it does not perform reference counting---there is no automatic garbage collection. If you want some kind of basic reference counting features in C++, please consider using the shared_ptr<> class. You can get this class either through the Boost libraries or through C++0x standard headers. (Note: Do not confuse shared_ptr<> with auto_ptr<>. They are quite different. Do not use auto_ptr<> with STL containers!)
 

jbode

Ars Legatus Legionis
11,754
Subscriptor
lobobo":2ahla5g3 said:
So at this point I'm operating under the assumption that there are two deques, each filled with pointers to the same single set of five pairs in memory.

If I call the .clear() method on one of those deques, shouldn't the pointers in the other deque dangle? In other words, .clear() clears the elements in the deque and for each pointer in the deque it deletes the content in memory pointed to by the pointer.

No. Deleting a pointer does not delete the thing it points to. They are separate objects with separate lifetimes.

lobobo":2ahla5g3 said:
Note: If I iterate through the deque and call delete on each element, the outcome is as I expected the above to be. Each element now points to a pair that doesn't exist.

Exactly.

lobobo":2ahla5g3 said:
So is it safe to say that my assumption was wrong? That calling the .clear() method on a deque doesn't call delete on its elements? I assumed calling the destructor implied that delete was being called on it...

Yes, you've deleted the elements in the deque; however, all the deque contains are pointer values, which are distinct objects from the things they're pointing to.
 
Status
Not open for further replies.