Using auto_ptr with char*

Discussion in 'C++' started by Vijay Bajwa, Feb 7, 2007.

  1. Vijay Bajwa

    Vijay Bajwa Guest

    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
    Vijay Bajwa, Feb 7, 2007
    #1
    1. Advertising

  2. Vijay Bajwa

    Rolf Magnus Guest

    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*)::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.
    Rolf Magnus, Feb 7, 2007
    #2
    1. Advertising

  3. Vijay Bajwa

    Dennis Jones Guest

    "Rolf Magnus" <> wrote in message
    news:eqb6o6$u8g$02$-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
    Dennis Jones, Feb 7, 2007
    #3
  4. In article <>,
    "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?
    >
    > 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
    Howard Hinnant, Feb 7, 2007
    #4
  5. Vijay Bajwa

    Noah Roberts Guest

    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>.
    Noah Roberts, Feb 7, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens

    auto_ptr to char[ size_t]?

    Marc Schellens, Jul 16, 2003, in forum: C++
    Replies:
    8
    Views:
    3,272
    Karl Heinz Buchegger
    Jul 17, 2003
  2. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,534
    Dave Rahardja
    Jan 11, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,011
    Ian Collins
    May 9, 2006
  4. Tim H

    auto_ptr to char[]

    Tim H, Feb 1, 2008, in forum: C++
    Replies:
    10
    Views:
    715
    Howard Hinnant
    Feb 10, 2008
  5. Sousuke
    Replies:
    9
    Views:
    1,131
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page