Using auto_ptr with char*

V

Vijay Bajwa

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*)::eek: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
 
R

Rolf Magnus

Vijay said:
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*)::eek: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.
 
D

Dennis Jones

Rolf Magnus said:
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
 
H

Howard Hinnant

"Vijay Bajwa said:
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*)::eek: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_extensions/unique_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/wg21/docs/papers/2005/n1856.html#Additi
on%20-%20Class%20template%20unqiue_ptr

-Howard
 
N

Noah Roberts

Vijay said:
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>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top