About vectors
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: About vectors

  1. #1
    Join Date
    Jul 2009
    Posts
    153

    About vectors

    How does a vector know when to destroy itself?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: About vectors

    It's a class. It doesn't destroy itself, the destructor is called as soon as the class is deleted.

  3. #3
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    I'm assuming for each element inside a vector it's allocating memory internally.

    So, this is never deleted if you dont use the vector anymore? And thus vectors cause memory leaks?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: About vectors

    Quote Originally Posted by ProgrammerC++ View Post
    I'm assuming for each element inside a vector it's allocating memory internally.

    So, this is never deleted if you dont use the vector anymore? And thus vectors cause memory leaks?
    Of course not. The vector's memory is deleted when the vector is deleted or goes out of scope. If it's a vector of pointers, you need to delete the memory the pointers point to yourself.

  5. #5
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    so if you have

    Code:
    vector<T*> v;
    
    T * ptr = new T();
    
    v.push_back( ptr );
    
    delete ptr;
    
    //nothing with v done after this
    //leak?
    Last edited by ProgrammerC++; August 7th, 2010 at 04:02 PM.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: About vectors

    No since the only thing v contains is a pointer to the deleted T object.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  7. #7
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    Memory does not have to be allocated to store the pointer to the T object?

  8. #8
    Join Date
    Jun 2010
    Posts
    50

    Re: About vectors

    It's not about the vector, but about the object it contains. Vector is made to be a dynamic object container, there will be of course use of allocators to dispatch and reclaim memory automatically
    Right after you delete an allocated object the area it points to will be set free, all the later works can't inspire any more the provocation of the same pointer except overriding

  9. #9
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    So a memory leak occurs if I'm not clearing the vector

  10. #10
    Join Date
    Jun 2010
    Posts
    50

    Re: About vectors

    When did you allocate your vector ?
    It's not the vector that you are allocating, it's the object of type T you are to deal with.
    By the way, today I bought a sandwich. I have ketchup and chili sauces already

  11. #11
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    vector is allocated on the stack, it can't grow there, so it has to allocate memory to grow and hold elements like my pointer

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: About vectors

    Quote Originally Posted by ProgrammerC++ View Post
    vector is allocated on the stack, it can't grow there, so it has to allocate memory to grow and hold elements like my pointer
    It managers its own memory. If the vector contains pointers, it won't delete the what the pointers point to.

  13. #13
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    Yes but where does it store the pointer to the object? It must be in newly allocated memory as soon as push_back is called

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: About vectors

    Quote Originally Posted by ProgrammerC++ View Post
    Yes but where does it store the pointer to the object? It must be in newly allocated memory as soon as push_back is called
    Yes, that's correct. However, the vector cleans up after itself; that memory is automatically deallocated as soon as the vector goes out of scope or is otherwise itself destroyed.

    In short: If you allocate something, then you have to clean it up. If an object allocates something internally, then that object is expected to clean it up in its destructor (if not before). This, incidentally, is a guideline you should follow in creating your own classes as well.

    So a memory leak occurs if I'm not clearing the vector
    Actually, clearing doesn't affect things at all----the vector is designed not to deallocate memory when it's cleared, only when it's destroyed. If you actually want a vector to resize itself smaller, then you need to use the "swap trick". (Usually, there's no pressing desire to do this however, so it's more efficient to let the vector keep its excess memory just in case you ever add more to it.)
    Last edited by Lindley; August 7th, 2010 at 05:22 PM.

  15. #15
    Join Date
    Jul 2009
    Posts
    153

    Re: About vectors

    How does a vector know it goes out of scope?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center