Hello all,
I'm wondering if someone can explain "why this is" in C++.
Let's say there are two deques.
Let's say we use the new operator and a simple loop to add five elements to the first deque.
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.
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...
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...