How to cleanup array of structs?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 4 06:12:05 PDT 2017


On Sunday, 4 June 2017 at 12:24:44 UTC, Suliman wrote:
>> // 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?

it is not restricted to multithreads but is perhaps easiest to 
think about it in those terms.

so you do
> 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.


More information about the Digitalmars-d-learn mailing list