Solved

How to use auto_ptr with char*

Posted on 2001-09-18
8
1,154 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 40 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 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