Alternative destructors, do/should we have them?

Dukc ajieskola at gmail.com
Thu Jul 5 18:25:02 UTC 2018


On Thursday, 5 July 2018 at 16:12:08 UTC, Atila Neves wrote:
> On Thursday, 5 July 2018 at 10:57:51 UTC, Dukc wrote:
>> D does not have default constructors for structs, but I have 
>> thought that this might get around the problem:
>>
>> [...]
>
> You already have that capability:
>
> struct HandGrenade {
>     void* ptr;
>     void destroy() {
>         import core.stdc.stdlib: free;
>         free(ptr);
>     }
> }
>
>
> This isn't a great example since it's perfectly fine to pass 
> null to core.stdc.stdlib.free.
>
> Atila

On Thursday, 5 July 2018 at 16:12:08 UTC, Atila Neves wrote:
> This isn't a great example since it's perfectly fine to pass 
> null to core.stdc.stdlib.free.
>
> Atila

You're right. Well, It could be some custom allocation table that 
does not check for such things:

struct HandGrenade
{    void* ptr;
      ~this()
      {   foreach
         (   ref _byte;
             cast(ubyte[]) ptr[0 .. 
memoryTable.assumeSorted.upperBound(ptr).front - ptr]
         ) _byte = 0;
      }
}


More information about the Digitalmars-d mailing list