C++如何删除一个结构体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134323/
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
C++ how to delete a structure?
提问by Rella
Structure I created:
我创建的结构:
struct VideoSample
{
const unsigned char * buffer;
int len;
};
VideoSample * newVideoSample = new VideoSample;
newVideoSample->buffer = buf;
newVideoSample->len = size;
//...
How now to delete it now?
现在如何删除它?
回答by Amarghosh
delete newVideSample;
This won't free any memory you allocated to newVideoSample->buffer
though - you have to free it explicitly before deleting.
这不会释放您分配给的任何内存newVideoSample->buffer
- 您必须在删除之前明确释放它。
//Free newVideSample->buffer if it was allocated using malloc
free((void*)(newVideSample->buffer));
//if it was created with new, use `delete` to free it
delete newVideSample->buffer;
//Now you can safely delete without leaking any memory
delete newVideSample;
Normally this kind of freeing is written in the destructorof the class so that it'll be called automatically when you delete
the dynamically created object.
通常,这种释放是在类的析构函数中编写的,以便delete
在动态创建对象时自动调用它。
Thanks @steve for mentioning it :)
感谢@steve 提到它:)
回答by aschepler
delete newVideoSample;
But if the new
and delete
are in the same context, you're probably better off skipping them and just creating it on the stack instead:
但是如果new
和delete
在相同的上下文中,你可能最好跳过它们而只是在堆栈上创建它:
VideoSample newVideoSample = {buf, size};
In that case, no cleanup is necessary.
在这种情况下,不需要清理。
回答by SLaks
You're looking for the delete
keyword:
您正在寻找delete
关键字:
delete newVideoSample;
回答by vitaut
delete newVideoSample;
delete newVideoSample;
However, consider using a smart pointerthat will release the memory automatically, for example:
但是,请考虑使用会自动释放内存的智能指针,例如:
std::auto_ptr<VideoSample> newVideoSample(new VideoSample);
回答by Prasoon Saurav
Use delete
用 delete
VideoSample * newVideoSample = new VideoSample;
//.. stuffs
delete newVideoSample;
There is also an overloadi.e delete[]
还有一个重载即delete[]
VideoSample * newVideoSample = new VideoSample[n];
//.. stuffs
delete [] newVideoSample;
In Modern C++ it is always recommended to use smart pointers. You may want to use boost::shared_ptr<T>
from the boost library.
在现代 C++ 中,始终建议使用智能指针。您可能想boost::shared_ptr<T>
从 boost 库中使用。
回答by dripfeed
If you intended VideoSample to free its buffer
member then VideoSample is a fragile class. It has no way of knowing whether buf
was created in the heap using new[]
or malloc
, or is the address of a variable on the stack.
如果您打算 VideoSample 释放其buffer
成员,那么 VideoSample 是一个脆弱的类。它无法知道是否buf
是在堆中使用new[]
或 来创建的malloc
,或者是堆栈上变量的地址。
回答by robev
In C++ a structure is the exact same as a class except everything is public by default, where a class is private by default. So a structure can have a destructor and is freed with delete.
在 C++ 中,结构与类完全相同,除了默认情况下所有内容都是公共的,默认情况下类是私有的。所以一个结构可以有一个析构函数,并通过 delete 释放。
回答by Justin Niessner
Unless I'm missing something, you just use delete
:
除非我遗漏了什么,否则你只需使用delete
:
delete newVideoSample;
回答by Abyx
delete newVideoSample
.
In C++ struct
is the same as class
but with default public fields.
delete newVideoSample
. 在 C++struct
中class
与默认公共字段相同。
回答by Satyam
**You create object of Videosample , so you just use delete..
**您创建了 Videosample 的对象,因此您只需使用 delete..
VideoSample * newVideoSample = new VideoSample; delete newVideoSample;**
VideoSample * newVideoSample = new VideoSample; 删除新的视频样本;**