1

I'm writing a multiplatform application in RAD 10.3.3 and i have an array manipulation problem

type
    Tsex = (F,M);

    Tcheckup = record
        values : array of Tvalue;
        name: string;
        case integer of
            0 : (minV : array[F..M] of word;
                 maxV : array[F..M] of word;
                 monada : byte;);
            1 : (male : boolean);
        end;
var deiktes    : array of Tcheckup;
.....
// here i'm filling array "deiktes" with right values

....
// 0 < j  < length(deiktes)
//here i try to delete the item j, so i move all the following one step up
// to overwrite the j item

move(deiktes[j+1],deiktes[j],(length(deiktes)-1-j)*sizeof(TCheckUp)); (1)
// and then decrease the length of the array
setLength(deiktes,length(deiktes)-1); (2)

In debug mode i can see that the array "values" is as must it be after deletion. But continuing the execution the last item has random values in the array "values" (but right value in the string "name") I suppose that either (1) or (2) functions free the pointer of the array "values" so, when the app needs to store something else, it uses the freed pointer's memory. If so, what can i do to avoid this please?

8
  • If you want to delete an item, call the Delete function. That will do the job more simply and also doesn't suffer from the defect of your code which operates on a managed type without respecting reference counting. Commented Oct 29, 2020 at 20:23
  • Alternatively, you might want to use a TList<TCheckup> instead. Commented Oct 29, 2020 at 20:48
  • @david-heffernan but "deiktes" is a dynamic array of Tcheckup record. It hasn't any delete function Commented Oct 29, 2020 at 22:50
  • @Andreas Rejbrand seems quite appropriate. I will give it a try. Is it certain that this way I will not have a problem with the dynamic table "values" when I delete a member of the list? Should I not do setlength (values, 0) first? However, I still do not understand why the way I do it creates a problem. Can anyone explain it to me? Commented Oct 29, 2020 at 22:58
  • @Jim It's not a method of the array. It's a function called Delete. docwiki.embarcadero.com/Libraries/Sydney/en/System.Delete Commented Oct 29, 2020 at 23:11

0