Thread: Question about autro_ptr

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Question about autro_ptr

    Hello, I am using auto_ptr for the first time in a large project, but I have a question. How does an auto_ptr knows when to use delete[] or delete? I am using in like this:

    Code:
    auto_ptr<char> buffer(new char[buff_size]);
    In this case, auto_ptr should delete de pointer with delete[].

    What about:

    Code:
    auto_ptr<AnObject> buffer(new AnObject);
    Thanks any insight.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    From what I understand, auto_ptr stores a pointer to an object obtained via new, not new[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Would not make it almost useless?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Would not make it almost useless?
    No, since it can still be used to perform RAII with a single object, after all.

    Note that what you are trying to do by using auto_ptr with new[] can be accomplished with say:
    Code:
    std::vector<char> buffer(buff_size);
    Last edited by laserlight; 07-02-2007 at 09:00 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Well, if I am going to use a vector, using a pointer is no longer interesting, since I do not need dynamic allocation anymore.

    Thanks for the help.

    Edit: I forgot to ask. Can I really do something like:

    Code:
    vector<char> buff;
    
    // populate vector
    
    send(sock_fd, &buff[0], buff.size(); // send through socket connection
    Is it really safe and portable??
    Last edited by Mortissus; 07-02-2007 at 10:25 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is it really safe and portable??
    As far as I know, yes, at least where &buff[0] is concerned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The memory allocated for a vector is guaranteed to be contiguous. http://www.thescripts.com/forum/thread141366.html

    So I think that if you added an extra ')', that code would work just fine.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Ok! Thanks!

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The following from ATL could be useful:
    Code:
    CAutoVectorPtr
    CAutoPtr
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    ATL is a big (and platform-specific) dependency to pull in for mere containers.

    Try Boost.PtrContainer instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I do not know ATL, is it like Boost?
    Installing Boost would not worth it, since I would only need the auto_ptr functionality.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Boost is SOOOO useful when it comes to sweet pointer objects and lots of other nifty stuff, I use it for serialization too.

    I'd suggest using it as much as possible, it's one of the best libraries out there I think
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Then I shall take a look, thank you.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In addition, for most of the boost libraries you don't even have to build it. Just download and set your include path to the boost_1_34 (or whatever it is) directory and you can use the ptr_vector.

    Although in this case there's no need for the boost libraries, vector<char> is exactly what you want. The vector class does for arrays what auto_ptr does for regular pointers (auto_ptr has some weird differences, but basically this is correct).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM