C++ 如何从内存中正确释放std::字符串
当我使用完std::string后,从堆上分配的内存中删除它的最佳方法是什么?谢谢 如果它在堆上,则使用C++ 如何从内存中正确释放std::字符串,c++,string,C++,String,当我使用完std::string后,从堆上分配的内存中删除它的最佳方法是什么?谢谢 如果它在堆上,则使用删除,如果它在堆栈上,则不使用任何内容。std::string只是一个普通的类,因此通常的规则适用 如果将堆栈上的std::string对象分配为全局对象和类成员。。。您不需要做任何特殊的事情,当它们超出范围时,会调用它们的析构函数,它会自动释放用于字符串的内存 int MyUselessFunction() { std::string mystring="Just a string.
删除
,如果它在堆栈上,则不使用任何内容。std::string
只是一个普通的类,因此通常的规则适用
如果将堆栈上的std::string
对象分配为全局对象和类成员。。。您不需要做任何特殊的事情,当它们超出范围时,会调用它们的析构函数,它会自动释放用于字符串的内存
int MyUselessFunction()
{
std::string mystring="Just a string.";
// ...
return 42;
// no need to do anything, mystring goes out of scope and everything is cleaned up automatically
}
唯一需要做的事情是使用new
操作符在堆上分配std::string
;在这种情况下,与使用new
分配的任何对象一样,您必须调用delete
来释放它
int MyUselessFunction()
{
// for some reason you feel the need to allocate that string on the heap
std::string * mystring= new std::string("Just a string.");
// ...
// deallocate it - notice that in the real world you'd use a smart pointer
delete mystring;
return 42;
}
正如示例中所暗示的,通常在堆上分配std::string
是毫无意义的,并且,当需要时,仍然应该将此类指针封装在智能指针中,以避免内存泄漏的风险(在异常情况下,多个返回路径,…)
std::string
被定义为
namespace std
{
typedef std::basic_string<char> string;
};
名称空间std
{
typedef std::基本_字符串;
};
因此,它是basic_string
模板类实例化char
类型字符的同义词(这不会改变答案中的任何内容,但如果继续,即使是新手问题,你也必须学究)void foo(){
string*myString=新字符串(“在“delete myString;”上删除了堆分配的对象”);
不能把std::string当作任何基本类型
std::string *str = new std::string("whatever");
///code
delete str;
您可以像对待任何其他类一样对待std::string
。使用new
进行分配,完成后使用delete
。
对于C++11,我不建议在大多数情况下使用new
和delete
。如果需要在堆上分配字符串,请使用std::shared\u ptr包装它:
std::shared_ptr<std::string> my_string = std::make_shared<std::string>(std::string("My string"));
std::shared_ptr my_string=std::make_shared(std::string(“我的字符串”);
< > <代码> MySythStudio<代码>的所有副本都将超出范围,关联的内存将自动删除。@瑞安米切尔:1)通过阅读大多数C++书籍的前两章,可以轻松回答这个问题;2)有数千个确切的重复,3)措辞不佳,不清楚OP想问什么。首先,是的,我对C++是相当新的,而且我不使用C++书。我通过引用MSDN库来学习C++,并且无法找到任何特定的释放堆上的字符串的方法,我不确定使用Delphi函数是一个好主意,还是我应该使用FREST(& STR)。我可以看到为什么它可能已经收到了否决权,但是我以前尝试过谷歌和MSDN,并且无法找到一个好的答案,所以我决定在这里发布C++,我得到了我要找的答案。获得一本C++书并学习它。@ IDWMART:C++在语法上类似于C和C,而不是在成语中。@如果我说“我会飞这架飞机,我知道怎么开车,他们都有轮子”,你会笑的。你对C++或C到C++的知识是适用的。你不学习一种语言,假装他们有1-1的功能映射,它们有不同的原因。表现得好像你什么都不知道,学着那样。作为初学者,你是最后一个有权宣称你知道你在做什么的人,你不知道。e、 在这里,我认为std::string是模板类std::basic_string的typedef,具有char元素类型,而不是普通类。不是模板类的typedef(或结构或基本类型)对自动和动态对象的销毁和解除分配有任何影响。@Matteo,这仍然是错误的。这不是一个专门化,而是一个实例化。尤其重要的是,在新手问题上要学究式,否则新手很容易学会错误的术语。我不在乎这是专门化还是insta提示,但新手可能会认为“专门化==typedef”这看起来很合理,但显然是错误的。@Sergey Tachenov:std::basic_string
是一种专门化,typedef
是它的同义词;既然他说他知道C,我想typedef
的作用应该是显而易见的;不过,还是修复了,下次你可以自己编辑了。@Matteo,看起来不错好像我在这里也是一个新手。我一直认为“专门化”只指使用模板语法的显式专门化或部分显式专门化。事实并非如此,很抱歉有误导性的评论。@Sergey:严格地说,这是模板实例化,但我已经看到了这两个术语的用法(在这种情况下,显式实例化用于您所说的内容)。
int MyUselessFunction()
{
std::string mystring="Just a string.";
// ...
return 42;
// no need to do anything, mystring goes out of scope and everything is cleaned up automatically
}
void foo() {
string myString("stack-allocated string is automatically deleted when myString goes out of scope");
cout << myString << endl;
}
std::string *str = new std::string("whatever");
///code
delete str;
std::shared_ptr<std::string> my_string = std::make_shared<std::string>(std::string("My string"));