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
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
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
Re: Question about Vector Erase
Quote:
Originally Posted by
ABM
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.
Re: Question about Vector Erase
Thanks for your help. I learned a lot today.
Re: Question about Vector Erase
Quote:
Originally Posted by
ABM
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
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
Re: Question about Vector Erase
Quote:
Originally Posted by
ABM
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.
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.