C++ 中的动态内存分配
在动态内存分配方面,“删除”和“处置”C++ 运算符之间有什么区别?
What is the difference between the 'delete' and 'dispose' C++ operators with regards to dynamic memory allocation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
delete
将释放在非托管 C++ 中动态分配的内存Dispose
将强制自定义对象在托管 C++/CLI 中实现一次性对象的维护delete
will free memory dynamically allocated in unmanaged C++Dispose
will force custom object implemented maintenance of disposable objects in managed C++/CLI标准 C++ 中没有
dispose
运算符(或标准函数,或类似的任何东西)——您是否正在考虑某种特定的专有框架,例如 Microsoft 的 .NET“托管 C++”?delete
是标准,它确实是一个运算符,并且必须表现在任何符合标准的实现中都像这样——这是与运算符dispose
的主要区别,也是与运算符unicorn
的主要区别code>、toothfairy
和santaclaus
,它们有很多共同点,因为这些都不存在!-) [[对不起弗吉尼亚... !]]There's no
dispose
operator (or standard function, or anything of that ilk) in standard C++ -- are you thinking of some specific proprietary framework, such as Microsoft's .NET "managed C++"?delete
is standard, it's indeed an operator, and must behave like this in any standard conforming implementation -- that's the main difference with operatordispose
, and also the main difference with operatorsunicorn
,toothfairy
, andsantaclaus
, which have a lot in common since none of those exists!-) [[Sorry Virginia...!]]我假设这与 .NET 托管 C++ 有关,因为标准 C++ 没有“处置”的意义。
删除会释放用于存储对象的内存;该内存返回到堆并可用于其他存储需求。
Dispose 将使对象有机会释放它获取的资源,例如文件句柄等。标准 C++ 将在析构函数中看到此类任务完成。
I'm assuming this is related to .NET managed C++ because standard C++ has no sense of "dispose".
Delete will release the memory used to store the object; this memory returns to the heap and can be used for other storage requirements.
Dispose will give the object the chance to release resources it acquired such as file handles, etc. Standard C++ would see this sort of task done in the destructor.