std::vector< boost::shared_ptr<int> >::pop_back()
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed? i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope. example code: the reference count is decremented when the item is popped from the vector. boost::shared_ptr<int> item1(new int(1)); std::vector< boost::shared_ptr<int> > vect1; vect1.push_back(item1); std::cout << item1.use_count() << std::endl; vect1.pop_back(); std::cout << item1.use_count() << std::endl;
On Mon, Jul 10, 2006 at 03:58:19PM +1000, bringiton bringiton wrote:
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed?
i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope.
just look at the include files: bits/stl_vector.h says: void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); } so the destructor is called in std::_Destroy. Which can be found in bits/stl_construct.h: /** * @if maint * Destroy the object pointed to by a pointer type. * @endif */ template<typename _Tp> inline void _Destroy(_Tp* __pointer) { __pointer->~_Tp(); } It just calls the destructor.
just look at the include files:
i looked at the header, but got a little confused.
i don't see how you can call a destruct of a a memory space that still
exists. ie a C version of a vector.
shared_ptr<int> list[1024];
int n = 0;
// add an item
shared_ptr<int> newItem(new int(1));
list[n++] = newItem;
// remove the tail item
n--;
// destructor never called
is it possible to call a destructor of an object that exists?
what then happens then the object goes out of scope? the destructor will be
called twice.
(or maybe i am completely missing the point)
BTW: not sure if above code compiles (did it in my head)
On 7/10/06, Boris Breidenbach
On Mon, Jul 10, 2006 at 03:58:19PM +1000, bringiton bringiton wrote:
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed?
i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope.
just look at the include files:
bits/stl_vector.h says: void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); }
so the destructor is called in std::_Destroy. Which can be found in bits/stl_construct.h: /** * @if maint * Destroy the object pointed to by a pointer type. * @endif */ template<typename _Tp> inline void _Destroy(_Tp* __pointer) { __pointer->~_Tp(); }
It just calls the destructor.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
holy! you learn something every day. you can call the destructor of an
object (before destruction)
this is a trap for me because i dont clean up my variables in destructors.
class A {
public:
A() {
counter = 0;
}
~A() {
counter++;
std::cout << "~A(): " << counter << std::endl;
}
int counter;
};
int main(int argc, char* argv[])
{
A a;
a.~A();
return 0;
}
NOTE: destructor gets called twice.
(so it's probably a good idea to NULL pointers in destructors)
On 7/11/06, bringiton bringiton
just look at the include files:
i looked at the header, but got a little confused. i don't see how you can call a destruct of a a memory space that still exists. ie a C version of a vector.
shared_ptr<int> list[1024]; int n = 0;
// add an item shared_ptr<int> newItem(new int(1)); list[n++] = newItem;
// remove the tail item n--; // destructor never called
is it possible to call a destructor of an object that exists? what then happens then the object goes out of scope? the destructor will be called twice.
(or maybe i am completely missing the point)
BTW: not sure if above code compiles (did it in my head)
On 7/10/06, Boris Breidenbach
wrote: On Mon, Jul 10, 2006 at 03:58:19PM +1000, bringiton bringiton wrote:
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed?
i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope.
just look at the include files:
bits/stl_vector.h says: void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); }
so the destructor is called in std::_Destroy. Which can be found in bits/stl_construct.h: /** * @if maint * Destroy the object pointed to by a pointer type. * @endif */ template<typename _Tp> inline void _Destroy(_Tp* __pointer) { __pointer->~_Tp(); }
It just calls the destructor.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Mon, July 10, 2006 16:00, bringiton bringiton wrote:
just look at the include files:
i looked at the header, but got a little confused. i don't see how you can call a destruct of a a memory space that still exists. ie a C version of a vector.
shared_ptr<int> list[1024]; int n = 0;
// add an item shared_ptr<int> newItem(new int(1)); list[n++] = newItem;
// remove the tail item n--; // destructor never called
is it possible to call a destructor of an object that exists? what then happens then the object goes out of scope? the destructor will be called twice.
You can call the destructor directly. To better understand why vector implementation does it, read about the "placement new" operator. Some free source to start with is InformIt's C++ Reference Guide (Memory Management): http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=28&rl=1 In this section is also an article (a small one) about Placement New operator: http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=38&rl=1 And tips and techniques section describes it in a more detailed manner: http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=160&rl=1 This section also has some links to other articles. With Kind Regards, Ovanes Markarian
On Mon, July 10, 2006 07:58, bringiton bringiton wrote:
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed?
i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope.
example code: the reference count is decremented when the item is popped from the vector.
boost::shared_ptr<int> item1(new int(1));
std::vector< boost::shared_ptr<int> > vect1; vect1.push_back(item1);
std::cout << item1.use_count() << std::endl; vect1.pop_back(); std::cout << item1.use_count() << std::endl;
vect1.pop_back() removes an element at the end of the vector. So the element is not there anymore. This is done using the std::allocator class. Even if item's memory remains (I assume a vector implementation can decide whether to keep the last chank for later insert or not), allocator calls destroy and the element is destroyed (its destructor is called, if it is a compound type (the case with shared_ptr)). If destructor of shared_ptr is called its use_count is decreased. With Kind Regards, Ovanes Markarian
participants (3)
-
Boris Breidenbach
-
bringiton bringiton
-
Ovanes Markarian