Silvio a Beccara wrote:
this would be useful, but I'd need it for at least a bidimensional array (don't know whether the number of discriminants refer to the dimensions). Could it be done? Would it set limits to the freedom of oter programmers?
If the inner dimension has a constant size it could work. If both sizes are discriminants (i.e., `type Foo (x, y: Integer) = array [1 .. x, 1 .. y] of Bar' or something like this), it couldn't because elements would have to be moved around in memory.
Sorry if I come back to you so soon,
Sorry, I can't write mails while I'm sleeping (yet) ... ;-)
but I am really stuck with my activity due to this problem. Basically, what I need is a way to freely resize a bidimensional array inside a procedure without losing its contents. I can copy it over to another array, but in doing so I create another new array inside the procedure, and the heap is again filled up.
Is there a quick and not too difficult solution to this problem?
procedure addoneket2 ( var ktsa: PMatr2Dob10; neact: integer; var lastket: integer );
var
i, j, itop, jtop, i1, i2, i3, i4, j1, j2, j3, j4: integer;
ktsb: Pmatr2Dob10;
begin
//create a new array, k4, one row longer //to accomodate the new vector. //lastket is the last element of the NEW array. //copy k3 to k4, //then redimension k3, //finally copy k4 back into k3.
inc ( lastket ); jtop := 4 * neact; new ( ktsb, lastket, jtop );
for i4 := 1 to lastket - 1 do begin for j4 := 0 to jtop do begin ktsb ^[ i4, j4 ] := ktsa ^[ i4, j4 ]; end; end;
new ( ktsa, lastket, 4 * neact );
for i4 := 1 to lastket - 1 do begin for j4 := 0 to jtop do begin ktsa ^[ i4, j4 ] := ktsb ^[ i4, j4 ]; end; end;
dispose ( ktsb ); //free heap memory by disposing of ktsb.
end; //end procedure
That's a bit too complicated. You don't need to copy the elements twice. After the first copying, just assign the pointer (`ktsa := ktsb'), and dispose of ktsa before that to avoid losing memory.
Still then any other pointer (outside of this procedure) that refers to the (old) value of ktsa will get invalid. There's no way around that, unless you use pointers to the pointer and double-indirection everywhere. But that's less efficient, so it should be avoided. Usually it's quite possible to ensure that there's only one reference to the data (at least at the time where they're resized), or to update any other references explicitly.
Frank