While I do think that shared_ptr is very smart, to be able to fix that code it would have to be ESP-equipped (not Extremely Smart Pointer - Extra Sensory Perception :-).

When the shared_ptr goes out of scope, the reference count will become zero, and the stored pointer will be deleted (which in turn will do Very Bad Things to the stack). To force this code to work, a custom deleter that does nothing could be used (but why?).

If the example correlates to some real code, the use of shared_ptr is unnecessary in the first place, because the int is allocated on the stack, and can't possibly go out of scope whilst in function foo.

Bjorn

> -----Original Message-----
> From: E. Gladyshev [mailto:egladysh@yahoo.com]
> Sent: den 21 mars 2002 06:25
> To: boost@lists.boost.org
> Subject: [boost] shared_prt, will it crash?
>
>
> A smart pointer question,
> will this code crash?
>
> //===============
>
> void foo( int* p );
>
> main()
> {
>    int x;
>
>    foo( &x );
>
>    return 0;
> }
>
> void foo( int *x )
> {
>    //foo() has no idea how 'x' has been allocated
>    //but shared_ptr<> is smarter then foo() is.
>    //It is a smart pointer after all.
>    shared_ptr<int> xp( x );
>
>    //... call some other functions with x or xp ...
>    //shared_ptr<> should make sure that the
>    //object is not deleted while foo() is using it.
>
>    *xp = 0;
> }
>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost