Thread: vector destruction question

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    vector destruction question

    If I have a class that has a vector in it like this:
    Code:
    sturct Node{
    
         std::vector<T> data;
    
        // ...
    
    };
    And I make 3 new Nodes from an existing node, and delete it like this:
    Code:
    Node* curr = root;
    
    // make three new nodes from curr
    
    delete curr;
    is there any memory leaks, or is all well?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The vector member variable will be destroyed by the destructor of the Node class and it will clean itself up and destroy the data inside it.

    If your vector holds pointers to allocated memory, those pointers will not be deleted though.

    Also, whether your Node destructor cleans up the other nodes or not can't be seen from the provided code, so there could be a memory leak there.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Also, whether your Node destructor cleans up the other nodes or not can't be seen from the provided code, so there could be a memory leak there.
    I'm making a 234 tree and each node has a vector (with no more than three keys, in my case denoted by T as in template <typename T>). The vector is not storing pointers to T's or references.

    Basically, when i need to "promote" a four node during insertion, an existing four node (a node that has a vector of size = 3, and four pointers to children nodes) I take the middle key, push it up to the parent node, and change pointers (to other nodes) as necessary.

    So basically, one "four node" becomes three "two nodes" and I just want to copy the data into the three new "two nodes", change pointers around, then delete the existing "four node" that curr points to. My concern is memory leaks, although none seem apparent as is, but it's difficult to test for memory leaks in this program...

    BTW: I have NOT implemented the Node destructor, does that make a difference?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dudeomanodude View Post
    BTW: I have NOT implemented the Node destructor, does that make a difference?
    All you have to remember is that you must delete what you new.
    Did you new anything inside the constructor? No? Then you probably don't need a destructor.
    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"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by dudeomanodude View Post
    Basically, when i need to "promote" a four node during insertion, an existing four node (a node that has a vector of size = 3, and four pointers to children nodes) I take the middle key, push it up to the parent node, and change pointers (to other nodes) as necessary.
    This description (particularly "four pointers to children nodes" and "change pointers") implies the possibility of losing track of a node, unless all of the nodes pointed to are in the std::vector<>. If all of the nodes are elements of the std::vector, you need to ensure that the pointers are updated correctly whenever the vector is resized (because the address of vector elements can change when a vector is resized). While that won't necessarily cause a memory leak, there is a worse problem: you have not excluded the possibility of dangling pointers (a pointer to an object that no longer exists), and any usage of such pointers yields undefined behaviour.
    Quote Originally Posted by dudeomanodude View Post
    So basically, one "four node" becomes three "two nodes" and I just want to copy the data into the three new "two nodes", change pointers around, then delete the existing "four node" that curr points to. My concern is memory leaks, although none seem apparent as is, but it's difficult to test for memory leaks in this program...
    If it's difficult to test, you need to find a way to analyse your code to ensure correct behaviour. Again, your statements "just want to copy the data into the three new "two nodes"" and "change pointers around" ring warning bells.

    On the information you have given, I'm assuming the pointers all point at an element of a std::vector<T>. Assuming that, you won't have a memory leak. However, I reiterate that the nastier potential problem is that of dangling pointers/references.

    There are two scenarios you need to consider. The first is one Node containing pointers to an element of a vector in another Node: if one of those Nodes is destroyed, the other ends up with dangling pointers. Any subsequent dereferencing of those pointers will yield undefined behaviour.

    The second scenario to consider is any operation that resizes your vector, but does not update pointers accordingly.

    Quote Originally Posted by dudeomanodude View Post
    BTW: I have NOT implemented the Node destructor, does that make a difference?
    Not if all the pointers point at elements of the vector, and those elements will be implicitly destroyed with the Node. The default destructor will not address any issues of dangling pointers.

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