Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using auto_ptr with char*

Reply
Thread Tools

Using auto_ptr with char*

 
 
Vijay Bajwa
Guest
Posts: n/a
 
      02-07-2007
Say I have a const char* msg which is pointing to memory gotten by
something like (msg = new char[100] ). Then if I declare thus:

auto_ptr<const char> smartptr (msg) ;

Then will this bomb royally, when, on destruction, auto_ptr tries to
call delete on msg instead of delete [] ? Well, not bomb, by will
probably only free up 1 char worth of memory?

Is the way around it to make sure you get memory thus?
char* msg = (char*):perator new (100) ;

if that's the case, and if msg is being returned from a library,
callers must be very sure to call delete and not delete []. Not
elegant, since a pogrammer error like this won't be flagged by a
compiler. Is there a best practise?

Thanks in advance!
Vijay

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-07-2007
Vijay Bajwa wrote:

> Say I have a const char* msg which is pointing to memory gotten by
> something like (msg = new char[100] ). Then if I declare thus:
>
> auto_ptr<const char> smartptr (msg) ;
>
> Then will this bomb royally, when, on destruction, auto_ptr tries to
> call delete on msg instead of delete [] ?


Yes. std::auto_ptr is not suitable for arrays.

> Well, not bomb, by will probably only free up 1 char worth of memory?


Either that or anything else.

> Is the way around it to make sure you get memory thus?
> char* msg = (char*):perator new (100) ;
>
> if that's the case, and if msg is being returned from a library,
> callers must be very sure to call delete and not delete []. Not
> elegant, since a pogrammer error like this won't be flagged by a
> compiler.


But it might be by memory debuggers. valgrind for example does find places
where you attempt to use delete instead of delete[] on an array.

> Is there a best practise?


I think boost offers an array version of auto_ptr.

 
Reply With Quote
 
 
 
 
Dennis Jones
Guest
Posts: n/a
 
      02-07-2007

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:eqb6o6$u8g$02$(E-Mail Removed)-online.com...
>
> I think boost offers an array version of auto_ptr.


Yes, it does. At least two flavors:

boost::scoped_array
boost::shared_array

If the desire is to return a smart pointer from a library, choose
shared_array, because it is reference counted.

- Dennis


 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      02-07-2007
In article <(E-Mail Removed) .com>,
"Vijay Bajwa" <(E-Mail Removed)> wrote:

> Say I have a const char* msg which is pointing to memory gotten by
> something like (msg = new char[100] ). Then if I declare thus:
>
> auto_ptr<const char> smartptr (msg) ;
>
> Then will this bomb royally, when, on destruction, auto_ptr tries to
> call delete on msg instead of delete [] ? Well, not bomb, by will
> probably only free up 1 char worth of memory?
>
> Is the way around it to make sure you get memory thus?
> char* msg = (char*):perator new (100) ;
>
> if that's the case, and if msg is being returned from a library,
> callers must be very sure to call delete and not delete []. Not
> elegant, since a pogrammer error like this won't be flagged by a
> compiler. Is there a best practise?
>
> Thanks in advance!
> Vijay


Fwiw, here's a C++03 emulation of the proposed std::unique_ptr:

http://home.twcny.rr.com/hinnant/cpp...ue_ptr_03.html

(free, take it, no copyright or acknowledgment required) Requires boost
headers but not boost binary.

Assuming you've got paths to boost headers, and copy/paste in the code
at the above link, you can:

int main()
{
const char* msg = new char[100];
unique_ptr<const char[]> smartptr(msg);
}

The semantics of unique_ptr are very similar to auto_ptr (and so is the
overhead). Include the "[]" in the template argument to specify a
unique_ptr to array.

The best documentation (yet still unsatisfactory) is found here:

http://www.open-std.org/jtc1/sc22/wg...56.html#Additi
on%20-%20Class%20template%20unqiue_ptr

-Howard
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-07-2007
Vijay Bajwa wrote:
> Say I have a const char* msg which is pointing to memory gotten by
> something like (msg = new char[100] ). Then if I declare thus:
>
> auto_ptr<const char> smartptr (msg) ;
>
> Then will this bomb royally, when, on destruction, auto_ptr tries to
> call delete on msg instead of delete [] ? Well, not bomb, by will
> probably only free up 1 char worth of memory?


The better way to provide an RAII wrapper for char* is to use vector<char>.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
Pimpl using auto_ptr Keith Willis C++ 9 11-08-2007 08:35 PM
Dynamic multidimensional array using auto_ptr Skay C++ 8 06-09-2006 12:09 AM
About default copy constructor when using auto_ptr as the member PengYu.UT@gmail.com C++ 4 03-28-2006 05:26 AM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM



Advertisments