0

How to I remove a varible from memory? A friend and I were talking and he seems to think that just resetting it to "" would be enough. Is he correct? If not, how do I remove varibles when I'm done with them?

3
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Puckdropper
0

How to I remove a varible from memory? A friend and I were talking and he seems to think that just resetting it to "" would be enough. Is he correct? If not, how do I remove varibles when I'm done with them?

That is not the right way.....
We can't even apply delete to statically allocated objects.....but we can restrict their existence within scope so that whenevr the scope ends...destructors of the local objects are called automatically.
Example

int main()
{
int x=20;
{
   int y=10;
}//compiler automatically destroys variable after the scope..i.e y is destroyed
return 0;
}//x is also destroyed now
0

sunnypalsingh is right. Variables you declared will go away after you exit the scope they're declared in.

If you want to do something slightly different, where you treat the variable as deleted, you can set the variable to an unused value and keep going. A common example of this is to set an integer to a positive value if some condition is true and a negative value if false.

Some times, doing this is more useful than getting completely rid of the variable.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.