如何将MOVE用于具有记录作为元素且其上具有动态数组字段的动态数组? [英] How to use MOVE for dynamic arrays with a record as an element and a dynamic array field on it?

查看:77
本文介绍了如何将MOVE用于具有记录作为元素且其上具有动态数组字段的动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi Rio,并且我的程序有很多动态数组操作。为了提高某些长数组副本的速度,我尝试使用Move。对于基本类型(实数,整数)的一维动态数组,我可以管理Move的使用,但是对于以记录为元素的动态数组,以及具有另一个动态数组字段的该记录,我很难使其工作。发出命令后,我将MOVE用于记录的动态数组文件的方式,这些文件继续指向源数组的原始源地址。

I'm using Delphi Rio and my program has a lot of dynamic arrays operations. In order to improve speed of some long array copy, I tried to use Move . For 1D dynamic arrays of basic types (real, integer) I could manage the use of Move, but for a dynamic arrays with a record as its element and this record with another dynamic array fields I'm having trouble to make it work. The way I' using MOVE for the dynamic arrays fileds of records, after issue the command , these fileds continue pointing to the original source address of the source array.

测试过程:array_A是记录的动态数组,该记录的两个文件是动态数组。发出Move(array_A [0],B_array [0],sizeof(Array [0])* length(arra_A))之后,然后更改array_B动态数组字段中的值,这也会导致array_A的值也发生变化!两者都指向相同的地址。如何使用MOVE更改此行为?

Testing procedure : array_A is dynamic array of records, tow fileds of this record are dynamic arrays. After issue Move(array_A[0],B_array[0],sizeof(Array[0]) * length(arra_A)) then change values in array_B dynamic arrays fields, this cause changes in values of array_A too ! Both are pointing to the same address. How to change this behaviour using MOVE ?

请参阅我的代码:

    program ProjArrayMove;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

Type

     Arrayint   = Tarray<Integer>;
     Arrayreal  = TArray<Real>;

     RECmember = record
           mcode       : integer;
           mname       : string[30];
           mdynarr_int  : arrayint;
           mdynarr_real : arrayreal;
     end;

     ArrayMember = array of RECMember;

     Procedure FillsRecord (var pRec : RECmember; pSize : integer; pFactor : real);
     // Given a pRec , fills each dynamic array fields with pSize numbers
     var
        idx : integer;
     begin
          setlength(pRec.mdynarr_int,pSize);
          setlength(pRec.mdynarr_real,pSize);
          with pRec do
          begin
               for idx := 0 to pSize - 1 do
               begin
                    mdynarr_int[idx]  := idx;
                    mdynarr_real[idx] := idx * (1.0 + pFactor);
               end;
          end;
     end;


     Procedure FillsArray  (var pArr : ArrayMember; pSizeArray, pSizeRec : integer; pChar : Char);
     // Given a array of records pArr  , fills the array with pSizeArray records
     var
        idx : integer;
     begin
           setlength(pArr,pSizeArray);

           //Fils first record - element 0
           FillsRecord(pArr[0],pSizeRec,2.3);
           pArr[0].mcode := 0;
           pArr[0].mname := '0' + pChar;

           //Fills major array with records , each record with dynamic arrays fields also filled
           for idx := 1 to High(pArr) do
           begin
                FillsRecord(pArr[idx],pSizeRec,idx * 2.3);
                pArr[idx].mcode := idx;
                pArr[idx].mname  := idx.ToString + StringOfChar(pChar,2);
           end;
     end;


     Procedure PrintArray(pArr : ArrayMember);
     // Given an array of records pArr, prints each element including the dynami array fields
     var
        i,j : integer;
     begin
           Writeln(stringofchar('=',20));
           for I := Low(pArr) to High(pArr) do
           begin
                 Write('Idx :' + i.ToString + ' ' );
                 Write('Code :' + pArr[i].mcode.ToString  + ' ' );
                 Write('Name :' + pArr[i].mname + '   ');
                 Write(' mdynarr_int :');
                 for j := Low(pArr[i].mdynarr_int) to High(pArr[i].mdynarr_int) do
                     Write(' ' + pArr[i].mdynarr_int[j].ToString);
                 Write(' mdynarr_real :');
                 for j := Low(pArr[i].mdynarr_real) to High(pArr[i].mdynarr_real) do
                     Write(' ' + FloatToStr(pArr[i].mdynarr_real[j]));
                 Writeln;
           end;
     end;

var
    larray_A, larray_B : ArrayMember;
    idx, lsizeA, lsize_int, lsize_real : integer;
begin
      try
          //Step 1 - Fills the first array with 10 records and its relaed dynamic arrays fields with 5 elements each
          FillsArray(larray_A,10,5, 'A');

          //Step 2- An attempt to ast copy elements of larray_A to larray_B
          Setlength(larray_B, length(larray_A));
          lsizeA := Sizeof(larray_A[0]);

          MOVE(larray_A[0], larray_B[0], length(larray_A) * lsizeA);

          Writeln('========== ARRAY A ==========');

          PrintArray(larray_A);
          readln;

          Writeln('========== ARRAY B ==========');

          PrintArray(larray_B);
          readln;

          //Now change values in item of array B
          larray_B[0].mcode := 777;
          larray_B[0].mname := 'B was changed';
          larray_B[0].mdynarr_int[0]  := 427;
          larray_B[0].mdynarr_real[0] := 784.96;

          Writeln('======= ARRAY B AFTER CHANGES ========');

          PrintArray(larray_B);
          readln;

          Writeln('====== VERIFYING IMPACT IN ARRAY A =======');

          PrintArray(larray_A);
          readln;

          //3-Attemp to use MOVE to copy contet of dynamic arrays fields in records member
          lsize_int := Sizeof(larray_A[0].mdynarr_int[0]);
          lsize_real:= Sizeof(larray_A[0].mdynarr_real[0]);

          for idx := Low(larray_B) to High(larray_B) do
          begin
                setlength(larray_B[idx].mdynarr_int,length(larray_A[idx].mdynarr_int));
                **MOVE(larray_A[idx].mdynarr_int[0],larray_B[idx].mdynarr_int[0],
                     lsize_int * length(larray_B[idx].mdynarr_int) );**

                setlength(larray_B[idx].mdynarr_real,length(larray_A[idx].mdynarr_real));
                MOVE(larray_A[idx].mdynarr_int[0],larray_B[idx].mdynarr_int[0],
                     lsize_real * length(larray_B[idx].mdynarr_real) );
          end;

          Writeln;
          Writeln;

          //Now change values in item of array B
          larray_B[0].mcode := 555;
          larray_B[0].mname := '2nd Change in B';
          larray_B[0].mdynarr_int[0]  := 10427;
          larray_B[0].mdynarr_real[0] := 10784.96;

          Writeln('======= ARRAY B AFTER 2nd CHANGES ========');

          PrintArray(larray_B);
          readln;

          Writeln('====== VERIFYING IMPACT IN ARRAY A =======');

          PrintArray(larray_A);
          readln;

          Writeln('==> It seems that MOVE in record fields of dynamic array type does not work as I expected - WHY ?');
          readln;

      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
end.

谢谢大家!

推荐答案

您第一次调用 Move 会获取内部数组的副本引用,但在编译器后面进行操作,因此破坏了引用计数。从那时起,您所做的所有其他事情注定会失败。您不能对受管理的数据使用移动

Your first call to Move takes a copy of the inner array references but does behind the back of the compiler and therefore disrupts reference counting. From that point on everything else that you do it doomed to fail. You cannot use Move on data that is managed.

要正确执行此操作,您需要如下代码:

To do this correctly you need code like this:

type
  TMyRec = record
    Int: Integer;
    Arr: TArray<Integer>;
    function Clone: TMyRec;
  end;

function TMyRec.Clone: TMyRec;
begin
  Result.Int := Int;
  Result.Arr := Copy(Arr);
end;

function CloneRecArray(const Arr: array of TMyRec): TArray<TMyRec>;
var
  i: Integer;
begin
  SetLength(Result, Length(Arr));
  for i := 0 to High(Result) do
    Result[i] := Arr[i].Clone;
end;

这篇关于如何将MOVE用于具有记录作为元素且其上具有动态数组字段的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆