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?

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.