Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] delete shared_ptr
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2011-03-16 16:23:04


On Wed, Mar 16, 2011 at 7:23 PM, pavel novikov <paul.cpprules_at_[hidden]>wrote:

> i'm excited to present you a technique which i occasionally invented
> shared pointers are very good helpers
> they help us to manage dynamically allocated memory
> however it seems so natural i have never seen so far that one be
> allowed to delete shared pointer
> like this:
>
> shared_ptr<int>
> p = new int,
> q = p;
> delete p;
> assert(!p); //neither assertion fires
> assert(q);
>

Am I missing something?

> cat shared.cpp
#include <boost/shared_ptr.hpp>

int main( )
{
    boost::shared_ptr<int> p = new int, q = p;
    delete p;
    assert(!p); //neither assertion fires
    assert(q);
}
> g++ -I ~/Downloads/boost_1_45_0 -o shared shared.cpp
shared.cpp: In function ‘int main()’:
shared.cpp:5:40: error: conversion from ‘int*’ to non-scalar type
‘boost::shared_ptr<int>’ requested
shared.cpp:6:12: error: type ‘class boost::shared_ptr<int>’ argument given
to ‘delete’, expected pointer
>

- Rob.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk