Delete funktion

Hello!

Just found out that in a school assignment i did a while back,my remove function wasnt correct,but i dont see the error tho

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Handler::remove(string recipient,string content)
{

		for(int i=0;i<this->nrofgifts;i++)
		{
			if(recipient==this->gifts[i]->getRecipient()&&content==this->gifts[i]->getContent())
			{
				this->gifts[i]=this->gifts[this->nrofgifts-1];
				//delete this->gifts[i];
				this->nrofgifts--;
			}
		}
				
}


Apperently this function only overwrites my array,giving a memory leak(which i dont detect)
the // line is the one i added now,woundering if thats it to complete my code?

Hope someone can point me in the right direction,thanks in advance;)
Last edited on
Yes, except that you have the order wrong.
Explicitly writing this-> is unnecessary, by the way.

You should also break out of the loop as soon as you have found the item you want to delete.
If there can be more than one, you need to decrement i before the next iteration, or else you might miss some matching elements.
Last edited on
Allrighty,thank you.
Topic archived. No new replies allowed.