XBasic3000
XBasic3000

Reputation: 3486

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?

Upvotes: 3

Views: 4484

Answers (3)

Daniel Rikowski
Daniel Rikowski

Reputation: 72524

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);

Upvotes: 5

dummzeuch
dummzeuch

Reputation: 11252

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.

Upvotes: 4

Hasan S
Hasan S

Reputation: 287

you can use move procedure declared in the system unit : system.move(pDataSource^, MyData, sizeof(TAnalyzeInfo));

Upvotes: 1

Related Questions