Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy data from a pointer?

...
  PAnalyzeInfo = ^TAnalyzeInfo;
  TAnalyzeInfo = record
    pPitch: array of Single;
    pEnergy: array of Single;
    pPitchAccent: array of Single;
    pEnergyAccent: array of Single;
    pDicAccent: array of Single;
    pScore: array of Single;
    pBoundary: Integer;
    szRecWord: array of array of AnsiChar;
    nRecWordNum: Integer;
    nFrameNum: Integer;
  end;
...

I have pDataSource: PAnalyzeInfo which contains data and I want to copy it to a new independent variable. MyData : TAnalyzeInfo.

Is it possible to copy the whole structure or adding it one bye one?

like image 359
XBasic3000 Avatar asked Jul 30 '26 17:07

XBasic3000


2 Answers

In Delphi you can copy a record just by assigning it, thanks to compiler magic.

MyData := DataSource^;

The dynamic arrays are reference counted, so the assignment also takes care of the dynamic arrays as long as you don't need a real deep copy. With a simple assignment they just share the same memory.

If you don't want that you can copy them individually:

MyData.pPitch = Copy(pDataSource^.pPitch, Low(pDataSource^.pPitch), 
                                          High(pDataSource^.pPitch);
like image 153
Daniel Rikowski Avatar answered Aug 02 '26 14:08

Daniel Rikowski


No, the dynamic arrays cannot be copied with a single copy command. You will have to:

  1. Copy each non-array field
  2. For each array
    1. Create a new array of the correct size in the target
    2. Copy the array members across

It would be much easier if the arrays were static. In that case copying the whole memory block would be possible.

like image 27
dummzeuch Avatar answered Aug 02 '26 13:08

dummzeuch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!