Re: How to move one element of Dynamic Array of Objects



TeChNoInSiDe wrote:

The above code seems to be working correctly. The problem is when I
try to make the same thing with multi-dimensional arrays like this:

var

TestArray : Array of Array of TPanel;

Your code migth work with fixed arrays:
TestArray : Array[0..2][0..4] of TPanel;

With dynamic arrays, you must initialize all subarrays:
SetLength(TestArray, n);
for i := 0 to n-1 do
SetLength(TestArray[i], m);

System.Move(TestArray[i][j], TestArray[(i - 1)][j], SizeOf(TestArray)
* (Length(TestArray) - (VIv_i - 1) - 1));

You shouldn't do this, because it can affect the reference counting of the subarrays.


This code will either generate delayed runtime access violations or
when the main application is closed it generates access violation
errors or invalid pointer operations.

The FreeAndNil(TestArray[1]) most probably will cause an AV, because arrays are not TObjects. Only do
TestArray[i] := Nil;
or assign it another array.

You can delete reference counted array elements in an tricky way (see TStringList.Delete), but I'd recommend to move the arrays in an regular way:
for i := ...
TestArray[i] := TestArray[i+1];
Finally set the last (now unused) array element to Nil.

DoDi
.



Relevant Pages

  • Re: rank mismatch for unused dummy arguments
    ... access array elements ... type character with the C character kind, or is an element or substring ... of an element of an array that is not an assumed-shape or pointer array. ... but for assumed size rank is not required ...
    (comp.lang.fortran)
  • Arrays
    ... ArrayElement(String[] names, int index, String value) - is it OK? ... In the Main method define a String array called names and initialize it ... iterate through the array elements and print the array ... in the replaceArrayElement() method use the index to identify the array ...
    (comp.lang.java.help)
  • Re: What does Tcl lack?
    ... > traces on array elements, ... > If some Tk problem is solved very well with traces ... If the only good reason is to free up the syntax sugar for dicts, ...
    (comp.lang.tcl)
  • Re: gfortran & adjustable array: most values remain zero
    ... first n-1 dimensions of an n-dimensional array must match between caller ... the ranks don't even have to match, must less the dimensions. ... If the first dimensions of the actual and dummy ... the resulting correspondence of array elements is ...
    (comp.lang.fortran)
  • Re: Storing _Bool in a single bit
    ... >>> The problem that I see, from a standards point of view, is the sizeof ... Each element of an array has to have a distinct address. ... You can't pack array elements tighter than 1 per byte ...
    (comp.std.c)