Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that you pass either data OR pointer to a data to Method1/2. You should always pass data itself. May be you just forgot that dynamic array is a pointer itself? You should not pass A or Pointer(A) in your methods (A is dynamic array here). Pass A[0] or Pointer(A)^.</p> <pre><code>procedure Method1(const MyVar; size: cardinal); var Arr: array of byte; begin SetLength(Arr, size); CopyMemory(Pointer(Arr), @MyVar, Size); end; procedure Method2(var MyVar; size: cardinal); var Arr: array of byte; begin SetLength(Arr, size); Arr[0] := 1; Arr[1] := 2; Arr[2] := 3; Arr[3] := 4; CopyMemory(@MyVar, Pointer(Arr), Size); end; procedure TForm1.FormCreate(Sender: TObject); var I: Integer; A: array of Integer; begin I := $01020304; Method1(I, 4); // we pass data itself, not the pointer to it. Method2(I, 4); SetLength(A, 2); A[0] := 0; A[1] := $01020304; Method1(A[0], Length(A) * SizeOf(A[0])); // note, that we pass data, not pointer Method2(A[0], Length(A) * SizeOf(A[0])); // A or Pointer(A) is a pointer to array's data end; </code></pre> <p>If A is dynamic array: </p> <ol> <li>A[0] is the same as Pointer(A)^ and represents array's data.</li> <li>@A[0] is the same as Pointer(A) or just A and represents the array itself, which is pointer to it's data (and some tech info on negative offsets).</li> </ol> <p>If A is static array:</p> <ol> <li>A[0] is the same as A and represents the array itself, which is array's data.</li> <li>@A[0] is the same as @A and represents the pointer to array.</li> <li>Pointer(A) or Pointer(A)^ are meaningless.</li> </ol> <p>Note, that Arr in Method1/2 is a dynamic array too, that is why we cast it to pointer (CopyMemory asks pointers, not the data). If we want to use Move routine (which asks data), we should write Pointer(A)^ instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload