Solved

How to use auto_ptr with char*

Posted on 2001-09-18
8
1,120 Views
Last Modified: 2011-09-20
I'm trying to use the auto_ptr but I can't figure out how to get it to work with char*

Can some post an example
0
Comment
Question by:HillRatIn4433
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
auto_ptr is not designed to use with array. Note that, the destructor of auto_ptr
will delete  the  pointer to "an object or an primary type : char, int ..." not array.

    ~auto_ptr()
     {
           if (_Owns)
                delete _Ptr;  

          // note : syntax for deleting  array  ==> delete  [ ] _Ptrt
     }

Therefore, if you use  auto_ptr with array , you will have trouble.

For example
   
          main()
         {
                char *p = new char[200] ;
                auto_ptr<char>  X(  p );              
         }

At the time the program finished, destructor of X wil do
             delete _Ptr;      // _Ptr == p

however, what you expect  is    
            delete [ ] _Ptr
         
In conclusion, you can not use auto_ptr with array. If you want, you need to write
auto_array_ptr .

More information : http://gcc.gnu.org/onlinedocs/libstdc++/20_util/howto.html#1



0
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 40 total points
Comment Utility
0
 
LVL 4

Expert Comment

by:IainHere
Comment Utility
>>how to get it to work with char*
Well, if you're looking for a safe way of handling strings, why not use the std::string?

>>Can some post an example

#include <iostream>
#include <string>

int main()
{
  std::string   strExample;
  strExample = "Hello World!";
  std::cout << strExample << std::endl;
}
0
 

Author Comment

by:HillRatIn4433
Comment Utility
Thanks
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:HillRatIn4433
Comment Utility
IainHere,
Thanks, but I'm already familiar with string.
0
 
LVL 4

Expert Comment

by:IainHere
Comment Utility
Not that you have to answer, but what are you planning to do with this array of chars?  If it's not a string, why not use a std::vector<char>?  
I'm only asking because I can't think of a circumstance where I'd rather wrap an array of chars in an auto_array_ptr than use something else.
0
 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
IainHere ,

Here is a  stituation.  Assume we use  char *getString( ... )  of 3rd party library.  
The function requires us to delete return value after using it.

I mean
 
     char *p = getString(...);

    // access p .....

    // release memory as getString require
    delete [] p;

If we have auto_array_ptr, we can do


    auto_array_ptr<char> p (  getString(...)  );


0
 
LVL 4

Expert Comment

by:IainHere
Comment Utility
Fair Nuf :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now