i.e. . .
#include<iostream>
void EatFrisky(Cat *Cat);
int main()
{
Cat * Frisky = new Cat;
EatFrisky(Frisky);
return 0;
}
void EatFrisky(Cat *Cat)
{
delete Frisky;
std::cout<<"Someone ate Frisky!\n";
}
i.e. . .
#include<iostream>
void EatFrisky(Cat *Cat);
int main()
{
Cat * Frisky = new Cat;
EatFrisky(Frisky);
return 0;
}
void EatFrisky(Cat *Cat)
{
delete Frisky;
std::cout<<"Someone ate Frisky!\n";
}
I believe that's right. When you call delete, you're deallocating the memory assigned to a pointer by the new keyword. As long as you pass the correct pointer, the memory will be correctly deallocated.
Edit:
EXCEPT: you need to call the pointer as Cat. Frisky isn't defined in EatFrisky.
oops. I didn't even notice I changed the name. Thank you though, that answers a biq question I had.
No problem.
This is also used in deconstruction of classes, mind you. If you have a class that uses 'new' in the constructor of the class, you need to make sure to put an appropriate delete in your deconstructor. If you don't...
MEMORY LEAK'D!!!oneone!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.