Sign in to follow this  
Silvo

Memory Management in C++

Recommended Posts

Silvo    166
I've got a question. In C++, if I decide I do not need a variable anymore, how do I remove it? EG:
int personAge;

std::cout << "Enter your age: ";
std::cin >> personAge;
std::cout << "Your name is " << personAge;

ok, not the best example, but in this, I have no further need of personAge. I suppose I could resuse it: EG:
int personAge;

std::cout << "\nEnter your age: ";
std::cin >> personAge;
std::cout << "\nYour name is " << personAge;

std::cout << "\nEnter your phone number: ";
std::cin >> personAge;   // personAge == phoneNumber?!?!
std::cout << "\nYour phone number is " << personAge;

See? doesn't seem practical. I could wait for it to go out of scope... EG:
int happyFunc()
{
   int cool;
   cool = 38;
   std::cout << "\nThe number " << cool << " is really cool!";
   std::cout << "\nAlso, I don't need \"cool\" anymore!";
}

OK for short functions, but what if it's main(), or if the function is really big?? back to my first example:
int happy;

std::cout << "On a scale of 1 - 10, how happy are you?\nHappiness factor: ";
std::cin >> happy;
std::cout << "\nYour happiness factor is " << happy << "\10";
//'happy' is now redundant.

std::cout << "\n\nblah blah blah...";

How to destroy 'happy'?

Share this post


Link to post
Share on other sites
Monder    993
If you explicitly allocate memory with new or malloc, you need to free it with delete or free (anything newed must be deleted and anything malloced must be freed). If you don't use new or malloc the variable will be destroyed when it goes out of scope. So in your example the personAge variable would be destroyed when you return from the function it is defined in you can't explicitly deallocate the memory it's using nor should you need to.

Share this post


Link to post
Share on other sites
mldaalder    193
You can use the scope to do that:


{
int happy;

std::cout << "On a scale of 1 - 10, how happy are you?\nHappiness factor: ";
std::cin >> happy;
std::cout << "\nYour happiness factor is " << happy << "\10";
//'happy' is now redundant.
}

std::cout << "\n\nblah blah blah...";



It doesn't even need a function body, just the brackets.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If you are going to explicitly create a scope like that then you might as place it in a well named function.


void CalculateHappyness()
{
int happy;

std::cout << "On a scale of 1 - 10, how happy are you?\nHappiness factor: ";
std::cin >> happy;
std::cout << "\nYour happiness factor is " << happy << "\10";
}

void CalculateAge()
{
int personAge;

std::cout << "\nEnter your age: ";
std::cin >> personAge;
std::cout << "\nYour name is " << personAge;
}

void CalculatePhoneNumber()
{
int PhoneNumber;
std::cout << "\nEnter your phone number: ";
std::cin >> PhoneNumber;
std::cout << "\nYour phone number is " << PhoneNumber;
}


int main()
{
CalculateHappyness();
CalculateAge();
CalculatePhoneNumber();
}



Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
But remember, an int is just 4 bytes, nothing compared to your 2 gigs or RAM. Don't worry about it.

Share this post


Link to post
Share on other sites
Silvo    166
Thanks Monder, mldaalder and Anonymous Poster 1 and 2 (or is it just 1?). Yes, I realise ints are very small, but what if i just need a temporary object (which could be very, very big) for a small section, but it remains in scope for a long time. The scoping trick {...} is a great idea, thanks. It just seems a little odd that you can't explicedly delete a variable (pointers excluded). Anywho, thanks for the info!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this