这是关于C++代码的记忆管理问题。
using namespace std;
#include <iostream>
#include <string.h>
int main()
{
string a="first";
string *b= new string;
*b=a;
a="second";
cout << *b << ", " << a;
delete b;
return 0;
}
We can deallocate the blocks of memory that stored the string that b pointed to. I m assuming this means that b no longer has any meaning once this is done. We can deallocate b to free some memory. Why can we not deallocate a? I know that we can only delete/free pointers, but the string a must take up some memory somewhere. Is there some way that we can free the memory that the string a takes up? If there are enough strings initialized in the same way that a is initialized, wouldn t it be possible that memory runs out?