Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use auto_ptr with char*

Posted on 2001-09-18
8
Medium Priority
?
1,248 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
ID: 6491837
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 160 total points
ID: 6491885
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6492851
>>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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HillRatIn4433
ID: 6493999
Thanks
0
 

Author Comment

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

Expert Comment

by:IainHere
ID: 6494195
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
ID: 6494512
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
ID: 6495767
Fair Nuf :-)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

971 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