[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Language War (Re: "C" Manual)



On Thu, 3 Jan 2002, Richard Cobbe wrote:

> Lo, on Thursday, January 3, William T Wilson did write:
> 
> Not in the general case, no.
> 
>     std::string *s = new string("foo");
>     std::string *s2 = s;
> 
>     delete s;
> 
> If we assume a variant of C++ that extends delete to set its argument
> pointer to NULL, you still have the problem of s2 hanging around.  In
> the general case, it's not so obvious that you've got two pointers to
> reset.


You can always overload new to set its pointer argument to the null
pointer value.

The allocated memory is released to the free store manager. There is
no leak. However, you have the dangling pointer s2, which you must
not apply the delete operator to again. This will result in at least
a segmenatation fault. 

For safety's sake, assign 0 to s2, so it will receive the null
pointer value.

BTW NULL is just the int value 0, it is not a pointer.

--David
David Teague, dbt@cs.wcu.edu
Debian GNU/Linux Because software support is free, timely,
                 useful, technically accurate, and friendly.
                 (I hope this is all of the above.)

> 





Reply to: