Boost logo

Boost Users :

From: David Walthall (walthall_at_[hidden])
Date: 2007-10-16 17:10:16


Peng Yu wrote:
> Hi,
>
> The following program runs well in my system. However, if I change 1
> to 0, it will terminate at the line with "delete". Does this mean that
> boost::shared_ptr can tell if the pointer is from the stack or the
> heap and does different things based on where the pointer is from?
>
> Thanks,
> Peng
>

Hi Peng,

No, shared_ptr still calls delete on 'a', which is undefined behavior.
Undefined behavior means just that: it is totally undefined what will
happen. It could crash, or continue merrily along, or anything else.
The following may show you what is going on better. If you put a
breakpoint (for VS 7.1) in boost/checked_delete.hpp in the
checked_delete(T * x) function, you'll see that it does call delete.
(It will also call delete in your original function, it just does it
when p goes out of scope, which is at the end of main.)

#include <iostream>
#include <boost/shared_ptr.hpp>

int main()
{
   int a[100];
   boost::shared_ptr<int> p(a);
   p.reset(); // calls delete here
   std::cout << std::endl;
}

David


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net