CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    136

    Question about Vector Erase

    Hi,
    I am erasing a object from the vector. I want to know that do i need to delete the object before I call the erase method or calling the vector erase method is suffice. Please see the following example which might explain better what I am asking.

    Class A{
    int val;
    };

    Class B
    {
    vector<A *> vectorA;
    void AddA(int val)
    {
    A *a = new A;
    a->val = val;
    vectorA.push_back(a);
    }
    //I am confused here what to do?
    // should i do this
    void DeleteVectorEntry(int nIndex)
    {
    vectorA.erase(vectorA.begin()+nIndex);
    }
    //or
    void DeleteVectorEntry(int nIndex)
    {
    A *a = vectorA.at(nIndex);
    if(a)
    {
    delete a;
    a = NULL;
    }
    vector.erase(vectorA.begin()+nIndex);
    }
    };

    I don't know which DeleteVectorEntry to use which makes sure that there is no memory leak in my program. Please help.

    I appreciate your help. I am a novice programmer.

    Regards,
    ABM

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Question about Vector Erase

    You need to explicitly free the memory ...

    Code:
    void DeleteVectorEntry(int nIndex)
    {
      if (nIndex>=0   &&    nIndex<vectorA.size())
      {  
        A *a = vectorA.at(nIndex);
        vector.erase(vectorA.begin()+nIndex);
      }
    }

    1. Depending on how you get nIndex, you might want to
    check that the index is valid or catch the exeption thrown by at()

    2. No need to check that "a" is not NULL in the delete or set "a" to NULL

  3. #3
    Join Date
    Jun 2010
    Posts
    136

    Re: Question about Vector Erase

    Thanks for your reply. I have couple of questions. Why i don't know need to check NULL. Suppose vector containing a null pointer there and deleting a NULL will create problems. Am i right?

    Second question is that could you please explain what would happen if just call vectorA.erase() and don't delete the pointer explicitly. What I am thinking calling VectorA.erase just delete the value in the memory. But it won't delete the pointer which is pointing towards that memory address. Am I right?

    Thanks once again.

    Regards
    ABM

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

    Re: Question about Vector Erase

    Quote Originally Posted by ABM View Post
    Thanks for your reply. I have couple of questions. Why i don't know need to check NULL. Suppose vector containing a null pointer there and deleting a NULL will create problems. Am i right?

    Second question is that could you please explain what would happen if just call vectorA.erase() and don't delete the pointer explicitly. What I am thinking calling VectorA.erase just delete the value in the memory. But it won't delete the pointer which is pointing towards that memory address. Am I right?

    Thanks once again.

    Regards
    ABM
    First question, no you're not right.

    Second question is backwards. You'll delete the pointer that's stored in the array, but what it pointed to will still be there.

  5. #5
    Join Date
    Jun 2010
    Posts
    136

    Re: Question about Vector Erase

    Thanks for your help. I learned a lot today.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question about Vector Erase

    Quote Originally Posted by ABM View Post
    Thanks for your help. I learned a lot today.
    To add to what others have stated, you could have seen that vector has no way to know whether the value stored in it was created with "new".
    Code:
    #include <vector>
    #include <stdlib.h>
    
    int main()
    {
       std::vector<int*> v;
       int x;
       v.push_back(&x);  // not created with new
    
       int *p1 = new int;
       v.push_back( p1 );  // created with new
    
       int *p2 = new int [10];
       v.push_back( p2 );  // created with new []
    
       int *p3 = (int *)malloc(10 * sizeof(int));
       v.push_back( p3 );  // created with malloc()
       //...
    }
    How does vector know that the first pointer wasn't created with "new", but then also supposed to know that the second value was created with new? How is the vector supposed to know that "delete[]" should be used on the third value instead of just "delete"? How does the vector know to call "free()" instead of "delete" for the fourth item? I could have added Windows API calls such as GlobalAlloc() to show you another example.

    So no, the vector does not know or care where that pointer comes from. The vector's job is to only remove memory it has allocated for itself to store the items. So it is always your responsibility to deallocate any memory you explicitly allocated yourself, as you're the only one who knows what function(s) you used to allocate the memory, and thereby know what function(s) to deallocate the memory.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 4th, 2012 at 12:23 PM.

  7. #7
    Join Date
    Jun 2010
    Posts
    136

    Re: Question about Vector Erase

    You all are full of knowledge. I wish one day I know about programming as you all do. Thanks once again. I got it now. Memory leak is a big problem in my software writing and everyday I am trying to learn so that it won't happen in to my software. I appreciate your help.

    Regards,
    ABM

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Question about Vector Erase

    Quote Originally Posted by ABM View Post
    Memory leak is a big problem in my software writing and everyday I am trying to learn so that it won't happen in to my software.
    One way is to use smart pointers, like std::shared_ptr. They help you manage heap objects and have them deleted when they should be deleted.

    Say for example the naked pointer you had in the vector were a shared_ptr instead, then if you erased it, and the object wasn't pointed at from somewhere else, it would've been deleted automatically.
    Last edited by nuzzle; April 4th, 2012 at 06:14 PM.

  9. #9
    Join Date
    Jun 2010
    Posts
    136

    Re: Question about Vector Erase

    Thanks nuzzle, I know about smart pointers but never got time to learn it yet. I will look in to it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured