Why is "delete" unsafe?

Elronnd elronnd at elronnd.net
Wed Sep 23 07:51:51 UTC 2020


On Wednesday, 23 September 2020 at 04:15:51 UTC, mw wrote:
> What do you mean by saying "it's definitely not safe" here?
>
> I mean: if I'm careful and know what I'm doing, e.g. remove all 
> the reference to  any part of the `object` before call 
> core.memory.GC.free(object), is there still any inherit 
> "unsafe" side of `free` I should be aware of?

'"delete" is unsafe' doesn't mean 'any program which uses 
"delete" is unsafe'.  What it means is, in a language that has 
'delete', /some/ programs will be unsafe.  If your language has 
delete, you cannot guarantee that programs will be safe.

Now, D is not safe by default ('delete' would definitely be 
disallowed in @safe code), but it still wants to have features 
that /encourage/ safety (fat pointers are a great example of 
this).

'delete' and 'free' are both equally unsafe; however, if you have 
verified that your usage of them is safe, it is fine to use them.


More information about the Digitalmars-d-learn mailing list