digitalmars.D.learn - How to cleanup array of structs?
- Suliman (15/15) Jun 02 2017 I remember that there was topic about remobing data from
- Biotronic (18/33) Jun 02 2017 There are multiple options
- Suliman (2/7) Jun 04 2017 Could you give an example where it can lead bugs? Do you mean
- Nicholas Wilson (11/26) Jun 04 2017 it is not restricted to multithreads but is perhaps easiest to
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?
Jun 02 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
Jun 02 2017
// 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();Could you give an example where it can lead bugs? Do you mean multi-thread apps?
Jun 04 2017
On Sunday, 4 June 2017 at 12:24:44 UTC, Suliman wrote:it is not restricted to multithreads but is perhaps easiest to think about it in those terms. so you do// 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();Could you give an example where it can lead bugs? Do you mean multi-thread apps?mytracks.length = 0; mytracks.assumeSafeAppend();and then you start building up the array again with some new data so that the array is sorted (or some other property of the array) and you do this over a period of time.foreach(i; iota(N)) { mytracks ~= MyTrack(i, "",0,0); // id is sorted Fibre.yield(); // do something else, maybe wating for more data } func(mytracks); // precondition that arg is sorted.meanwhile you have a reference to the `mytracks`buffer somewhere else (another global variable for instance) and it is not expecting to have its data 'stomped' and then it writes to it then it will this may make `mytracks` no longer sorted.
Jun 04 2017