reynaerde
asked on
Short question about copying array contents and pointer assignment
Considering the following:
TCell = packed record
status : cell_status;
to_update : cell_status;
lifetime: word;
resist: byte;
activated: boolean;
end;
type cell_pointer = ^TCell;
TMargolusBlock = packed record
cell : array[0..3] of cell_pointer;
end;
TMargolusArray = class(TObject)
mblock : array of array of TMargolusBlock;
constructor create(size : Integer);
destructor Destroy;
override;
end;
currentMargolusArray : TMargolusArray;
Then:
currentMargolusArray := MargolusArrayA;
doesn't work, I just get a TMargolusArray object with an array filles with nils.
How can I get the contents of MargolusArrayA in currentMargolusArray ? Or do I really need to copy the whole thing?
(like: for teller := 0 to ...: currentMargolusArray.cell[ 0] := MargolusArrayA.cell[0] etc..)
Furthermore, a bit related, what is the difference between:
type cell_pointer = ^TCell;
cpointer : cell_pointer;
cell : Tcell;
cpointer := @cell;
and
cpointer^ := cell;??
TCell = packed record
status : cell_status;
to_update : cell_status;
lifetime: word;
resist: byte;
activated: boolean;
end;
type cell_pointer = ^TCell;
TMargolusBlock = packed record
cell : array[0..3] of cell_pointer;
end;
TMargolusArray = class(TObject)
mblock : array of array of TMargolusBlock;
constructor create(size : Integer);
destructor Destroy;
override;
end;
currentMargolusArray : TMargolusArray;
Then:
currentMargolusArray := MargolusArrayA;
doesn't work, I just get a TMargolusArray object with an array filles with nils.
How can I get the contents of MargolusArrayA in currentMargolusArray ? Or do I really need to copy the whole thing?
(like: for teller := 0 to ...: currentMargolusArray.cell[
Furthermore, a bit related, what is the difference between:
type cell_pointer = ^TCell;
cpointer : cell_pointer;
cell : Tcell;
cpointer := @cell;
and
cpointer^ := cell;??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cpointer := @cell;
Here, cpointer is told to point to a reference pointer that is pointing at cell.
cpointer^ := cell;
Here, the reference that cpointer is pointing to is changed to reference to cell.
Pointers to pointers can make life real complicated. :-)
Here, cpointer is told to point to a reference pointer that is pointing at cell.
cpointer^ := cell;
Here, the reference that cpointer is pointing to is changed to reference to cell.
Pointers to pointers can make life real complicated. :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var
I:Integer;
begin
SetLength(Target, Length(Source));
for I := Low(Source) to High(Source) do Target[I] := Source[I];
end;
And if the array contains pointers to objects or records, then those things aren't copied either, just the references.
The thing you have to keep in mind is the difference between the actual data and references to data. For a better performance, you should just keep copying those references. Because copying data costs a little bit of time and the more data you have to copy, the more performance you'll lose. Do you really need to copy those arrays?