Link to home
Start Free TrialLog in
Avatar of pbavishi
pbavishi

asked on

string Deallocation

Guys,
I know string destructor class destroyes a string. Wanted to make sure, if (like in C free()) there is anything we need to explicitly call to deallocate the string in C++?

say:
#include <iostream>
#include <string>

void main()
{
std::string str("sad");
cout<<sad<<endl;

// Do we need to free the string here? or deallocate the assigned memory??

}

Please let me know.
ASKER CERTIFIED SOLUTION
Avatar of Exceter
Exceter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> std::string str("sad");
>> cout<<sad<<endl;

By the way, this will not compile. Your string is named str. sad is the text in your string.

Exceter
Avatar of Fallen_Knight
Fallen_Knight

no, if you don't use the new operator you don't need to free or delete anything at all.

if you had gone

string *str = new String("sad");
cout << str;

//you would have to go:
delete str;


even thou exiting the program free the memory anyway its best to always get into the habit of deleteing what you allocate with new