Thread: unsure about auto/smart pointers

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    unsure about auto/smart pointers

    Hello

    Im unsure how I should use smart/auto pointer in this case:

    I have a vector of pairs:

    Code:
    std::vector<std::pair<std::string, unsigned int>
    Now I want vector to be a pointer, so it would be deleted when not needed anymore..
    Should I make vector a smart/auto pointer, or each pair thats in vector smart/auto pointer or should I make both - vector and pairs pointers?

    I tried:
    Code:
    typedef boost::shared_ptr<std::vector<std::pair<std::string, unsigned int> >> record_ptr;
    record_ptr = new record_ptr(new (std::vector<std::pair<std::string, unsigned int> >));
    But this wont compile..

    Thanks for help

    Regards

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The compiler error helps to see, but...
    Code:
    typedef boost::shared_ptr<std::vector<std::pair<std::string, unsigned int> >> record_ptr;
    record_ptr = new record_ptr(new (std::vector<std::pair<std::string, unsigned int> >));
    Change that ">>" to a "> >", that's a problem with the C++ grammar.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    This wont compile either.

    Is this the right usage of smart pointers?
    Maybe I should consider using shared_array?

    The error:
    Code:
    error C2513: 'boost::shared_ptr<T>' : no variable declared before '='
            with
            [
                T=std::vector<std::pair<std::string,unsigned int>>
            ]

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    record_ptr = new record_ptr(new (std::vector<std::pair<std::string, unsigned int> >));
    Why are you allocating the smart pointer with new?

    Now I want vector to be a pointer, so it would be deleted when not needed anymore.
    Stack variables get removed when the stack frame exits. Any specific reason you can't do it this way?

    Should I make vector a smart/auto pointer, or each pair thats in vector smart/auto pointer or should I make both - vector and pairs pointers?
    Completely dependent on what you need the indirection for. (But note that it is forbidden to store auto_ptrs in containers.) However, the fact that you don't immediately know which the right way is suggests that you don't need either. See above.
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by l2u View Post
    Hello

    Im unsure how I should use smart/auto pointer in this case:

    I have a vector of pairs:

    Code:
    std::vector<std::pair<std::string, unsigned int>
    Now I want vector to be a pointer, so it would be deleted when not needed anymore..
    Should I make vector a smart/auto pointer, or each pair thats in vector smart/auto pointer or should I make both - vector and pairs pointers?
    Just use a vector directly. When you no longer want it to hold on to a large amount of memory, do this:
    Code:
    typedef std::vector<std::pair<std::string, unsigned int> > StringIntVec;
    StringIntVec m_myVec;
    m_myVec.resize(10000);
    // m_myVec uses lots of memory
    
    StringIntVec dummy;
    m_myVec.swap(dummy);
    // m_myVec no longer uses lots of memory
    You don't need to wrap a vector in a smart pointer. A vector already does the memory management for you. Of course if you have other reasons for wanting to use dynamic allocation of the vector variable itself, then by all means let us know.
    Last edited by iMalc; 07-12-2007 at 01:23 PM.
    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"

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Or to avoid the explicit dummy variable,
    Code:
    StringIntVec().swap(m_myVec);
    Controlling a Container’s Capacity

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by iMalc View Post
    You don't need to wrap a vector in a smart pointer. A vector already does the memory management for you. Of course if you have other reasons for wanting to use dynamic allocation of the vector variable itself, then by all means let us know.
    I want to wrap it in a smart pointer because I have a function that parses some data and puts it inside a vector.. I want this vector to be stored and used somewhere else in the program (the object that parses data is deleted after parsing is done). I still want to keep the vector, and dont want to copy it or pass the reference around..

    Isnt that the reason why should I wrap the vector with smart pointer?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. That makes sense. Wrapping the vector in the smart pointer is a better choice IMO than storing pointers in the vector.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That sounds like a reason to use an iterator, actually, although I really don't see the problem with passing a reference either. It's a lightweight and simple solution.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thank you guys for help!

    However, theres one thing that still isnt clear to me.

    Quote Originally Posted by CornedBee View Post
    But note that it is forbidden to store auto_ptrs in containers.
    Why would that be forbidden?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    auto_ptr's don't copy normally, they transfer ownership of the pointer. Since the vector will sometimes make several copies of an object, it is possible that the last copy made (the one with the ownership) is not the one stored in the vector. That means the copy stored in the vector is in a bad state and the data is lost or worse.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by l2u View Post
    I want to wrap it in a smart pointer because I have a function that parses some data and puts it inside a vector.. I want this vector to be stored and used somewhere else in the program (the object that parses data is deleted after parsing is done). I still want to keep the vector, and dont want to copy it or pass the reference around..

    Isnt that the reason why should I wrap the vector with smart pointer?
    I can see where you're coming from. Wanting to not copy the vector makes perfect sense.
    However, in order to keep the vector, you can just do the swap trick I just demonstrated. Swap the one that is about to be destroyed with the vector you want to keep it in. Internally it only involves swapping 3 pointer values and you have transferred the entire vector contents between the vectors. The lack of dynamically allocating a vector really doesn't hinder the kind of stuff you want to do at all so far.
    So, got any other reasons?
    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"

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In particular, what is the relationship between the final location of the vector and the object that parses data? Passing a reference to the vector to this object may well be the better solution.
    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

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
    class parser {
    public:
    	std::vector<std::string> vector_;
    };
    
    class cache {
    public:
    	std::map<std::string, std::vector<std::string> > map_;
    };
    
    //somewhere
    {
    parser p();
    p.parse();
    // now I have vector_ in parser class and want to store it in map (inside cache class) - parser is going to be destroyed
    }
    In this case, would swap be better option?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I like the shared_ptr option. You're storing it in the map, too, which matters a little. I don't think there's a ton of copying done with maps, but still, the container might be copying the value itself. Using a shared_ptr minimizes that copying.

    Oh, and apparently this whole thing will be much nicer in C++09. Can you wait til then?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM