|
-
April 4th, 2012, 11:04 AM
#1
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|