?
Solved

How to use auto_ptr with char*

Posted on 2001-09-18
8
Medium Priority
?
1,217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

719 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