C++ 删除C++中的字符串对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3130490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting string object in C++
提问by Rakesh K
I have a string object in my C++ program declared as follows:
我的 C++ 程序中有一个字符串对象,声明如下:
string str;
I have copied some data into it and done some operations. Now I want to delete the str object from the memory. I am not able to use delete operator since str is not a pointer. How do I delete that object from the memory to reclaim the memory allocated to it?
我已经将一些数据复制到其中并进行了一些操作。现在我想从内存中删除 str 对象。我无法使用 delete 运算符,因为 str 不是指针。如何从内存中删除该对象以回收分配给它的内存?
Thanks, Rakesh.
谢谢,拉克什。
回答by Dean Harding
You don't have to. When the string goes out of scope, it's destructor will be called automatically and the memory will be freed.
你不必。当字符串超出范围时,它的析构函数将被自动调用并释放内存。
If you want to clear the string right now(without waiting until it goes out of scope) just use str.clear()
.
如果你想清除字符串现在(无需等待,直到它的范围了),只要用str.clear()
。
回答by sbi
str.clear();
or
或者
str = "";
However, as stated in the comments, this does not guarantee (and, in fact, is rather unlikely) to actually return heap memory. Also not guaranteed to work, but in practice working well enough is the swap trick:
但是,正如评论中所述,这并不能保证(实际上,不太可能)实际返回堆内存。也不能保证工作,但实际上工作得很好是交换技巧:
std::string().swap(str);
Still, implementations employing the small string optimizationwill retain a few bytes of stack space (and str
itself of course also lives on the stack).
尽管如此,使用小字符串优化的实现将保留几个字节的堆栈空间(str
当然,它本身也存在于堆栈中)。
In the end I agree with all comments saying that it is questionable whyyou want to do that. Ass soon as str
goes out of scope, its data will be deleted automatically anyway:
最后,我同意所有评论,即您为什么要这样做是有问题的。一旦str
超出范围,它的数据无论如何都会被自动删除:
{
std::string str;
// work with str
} // str and its data will be deleted automatically
回答by pv.
Add an Additional scope
添加附加范围
{
String str;
...
...
}
to ensure that str goes out of scope when you no longer need it. Remeber it could be tricky in terms of how other variables are also defined.
确保 str 在您不再需要时超出范围。请记住,在如何定义其他变量方面可能会很棘手。
回答by SigTerm
Now I want to delete the str object from the memory.
现在我想从内存中删除 str 对象。
You can't. At least, you can't delete it completely. str is already allocated on stack (or in code segment if it is global variable), and it won't completely go away until you return from routine, leave the scope it has been created in (or until you exit the program - for global variable).
你不能。至少,你不能完全删除它。str 已经在堆栈上分配(或在代码段中,如果它是全局变量),并且它不会完全消失,直到您从例程返回,离开它已在其中创建的范围(或直到您退出程序 - 对于全局多变的)。
You can call .clear(), or assign empty string to it, but it doesn't guarantee that all memory will be freed. It guarantees that string length will be set to zero, but certain implementation may decide to keep part of originally allocated buffer (i.e. reserve buffer to speed up future operations, like += ).
您可以调用 .clear() 或为其分配空字符串,但不能保证所有内存都将被释放。它保证字符串长度将被设置为零,但某些实现可能决定保留最初分配的缓冲区的一部分(即保留缓冲区以加快未来的操作,如 += )。
Honestly, unless you have very small amount of available memory, I wouldn't bother. This is a very small object.
老实说,除非您的可用内存量非常小,否则我不会打扰。这是一个非常小的物体。