何时删除D? [英] When to delete in D?

查看:120
本文介绍了何时删除D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从C ++学习了8年。我的问题是关于D垃圾收集 - 我什么时候使用delete,何时不需要?

解决方案

你没有。删除不能与D版本2一起使用,并打算从语言中删除。这是什么,我不确定。相反,您使用一个函数销毁(object),该函数调用析构函数您可以释放非GC内存的资源。在GC收集对象自己的内存期间,析构函数将再次引发。这在中有解释。



这个想法是比GC提供的资源更早回收资源,并防止悬挂指针造成内存损坏。为了不太安全,在调用destroy(object)之后,core.memory模块提供了可用于释放内存的GC.free(object)。



我不是C ++程序员,我并不真正了解RAII模式,但如果您希望避免GC,那么这种引用计数是预期的策略。


I'm learning D from 8 years in C++. My question is with regards to D garbage collection - when do I use delete, and when don't I need to?

解决方案

You don't. Delete is not to be used with D version 2 and intended to be removed from the language. What the hold up is, I am not sure. Instead you use a function, destroy(object), which calls the destructor where you can free resources that are not GC memory. The destructor will be caused again during GC collection of the objects own memory. This is explained in "The D Programming Language".

The idea is to reclaim resources earlier than what the GC would provide and prevents memory corruption from dangling pointers. To be less safe the core.memory module provides GC.free(object) which can be used to free the memory, after calling destroy(object).

As I'm not a C++ programmer, I don't really know the RAII pattern, but this and reference counting is the expected strategy if you wish to avoid the GC.

这篇关于何时删除D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆