How to cleanup array of structs?

Biotronic via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 2 07:17:46 PDT 2017


On Friday, 2 June 2017 at 13:32:02 UTC, Suliman wrote:
> I remember that there was topic about remobing data from 
> struct/arrays of structs. But I do not remember what is 
> idiomatic way to do it, and can't google it.
>
> something like:
>     struct MyTrack
>     {
>         ulong id;
>         string recordDate;
>         int velocity;
>         int maxAllowedSpeedForRoad;
>     }
>
>     MyTrack mytrack;
>     MyTrack [] mytracks;
>
> // filling
>
> mytracks.clean() or what?

There are multiple options

// Will set the array to an empty one, and leave the
// old one for the GC to clean up when it feels like it.
// The safest way.
mytracks = null;

// Mostly equivalent:
mytracks = [];

// Will reuse the array, overwriting existing data.
// If other parts of the program are using existing data
// in the array, this will lead to hard-to-track-down bugs.
mytracks.length = 0;
mytracks.assumeSafeAppend();

// If you just want to get rid of the last element you added to 
the array:
mytracks = mytracks[0..$-1];

--
   Biotronic


More information about the Digitalmars-d-learn mailing list