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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
>> cout<<sad<<endl;
By the way, this will not compile. Your string is named str. sad is the text in your string.
Exceter