on Tue Oct 16 2007, "Peng Yu"
On 10/16/07, David Abrahams
wrote: on Tue Oct 16 2007, "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?
No. The termination is just an expression of the fact that you invoked undefined behavior by passing an illegal argument to delete.
Thanks, Peng
#include <iostream> #include
int main() { #if 1//try change this to 0 int a[100]; boost::shared_ptr<int> p(a); std::cout << std::endl; #else int a[100]; delete a; // would terminate here std::cout << std::endl; #endif }
I don't think my question was completely answered. Do you means reseting shared_ptr with an array from the stack is not defined?
Yes, that's true unless you also supply a custom deleter that has legal semantics. "delete a" is illegal unless a is dynamically allocated. However, it's not what I mean. I mean that "delete a" is undefined when a is a stack array. It's even not legal when it's a heap array allocated via "a = new int[SIZE]"; for that you'd need "delete[] a" (which also doesn't work when a is a stack array). -- Dave Abrahams Boost Consulting http://www.boost-consulting.com