Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: auto_ptr fo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 16 Topic 9517 of 9828
Post > Topic >>

Re: auto_ptr for array of built-ins

by "Martin T." <0xCDCDCDCD@[EMAIL PROTECTED] > Apr 22, 2008 at 11:13 AM

Carlos Moreno wrote:
> (...)
> 
> The thing is, the "buggy" code using auto_ptr does
> compile and run as expected --- even when linked with
> Electric Fence  (not 100% if electric fence does cover
> memory leaks as well as illegal memory accesses).  In
> any case, it runs without EFence objecting.
> 

You see, the thing is, for example on VS2005 or VS2008 the default
implementation of new[] and delete[] for PODs just go on to call malloc
and free so you could even call free(p) on a POD-array that was
allocated with new[] and it still would "work".
Of course: If someone start to overload new[] or you change the
compiler(version) or something else it might suddenly stop to work, and
you're left with broken, non-std compliant code that's a real pain to
fix ...

> I guess at this point I'm more curious than anything
> else, in that I think even doing something as silly as
> a simple class house_keeper (you know, to do the
> housekeeping upon return) with:
> 
> template <typename T>
> class house_keeper
> {
>      T * p;
> public:
>      house_keeper (T * p) : p(p) {}
> 
>      ~house_keeper() { delete [] p; }
> };
> 
> And then:
> 
>      char * result = new char [ ... ];
>      house_keeper<char> r (result);
>      // ...  (use the raw pointer result as needed)
>      return string (result, result + length);
> 
> Seems like I'm getting the very small required subset of
> auto_ptr or scoped_prt/array that provides me with memory
> management and exception-safety at a very low cost (...)

If you do not plan to include boost, implement a variant of scoped_ptr
and scoped_array yourself. I would certainly not call that silly!

template <typename T>
class house_kept_array
{
      T * p;
public:
      explicit house_kept_array (size_t n) : p(new T[n]) {}
      ~house_kept_array() { delete [] p; }
      T* get() { return p; }
};


br,
Martin

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-17 12:43:22 
Re: auto_ptr for array of built-ins
red floyd <no.spam@[EM  2008-04-17 16:19:42 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-17 16:22:24 
Re: auto_ptr for array of built-ins
markus2004x@[EMAIL PROTEC  2008-04-17 19:31:52 
Re: auto_ptr for array of built-ins
Carl Barron <cbarron41  2008-04-17 19:35:28 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-17 19:33:40 
Re: auto_ptr for array of built-ins
Tony Delroy <tony_in_d  2008-04-18 06:13:14 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-18 14:59:22 
Re: auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-18 15:02:34 
Re: auto_ptr for array of built-ins
Pete Becker <pete@[EMA  2008-04-18 17:16:33 
Re: auto_ptr for array of built-ins
Lance Diduck <lancedid  2008-04-18 17:17:38 
Re: auto_ptr for array of built-ins
mark.zaytsev@[EMAIL PROTE  2008-04-18 17:18:22 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-19 02:02:07 
Re: auto_ptr for array of built-ins
"Bo Persson" &l  2008-04-19 18:13:15 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-20 15:48:01 
Re: auto_ptr for array of built-ins
"Martin T." <  2008-04-22 11:13:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:43:42 CDT 2008.