Difference between revisions of "move"

From Lazarus wiki
Jump to navigationJump to search
(wikify)
 
Line 1: Line 1:
The [[Procedure|<syntaxhighlight lang="pascal" inline>procedure</syntaxhighlight>]] '''{{Doc|package=RTL|unit=system|identifier=move|text=<syntaxhighlight lang="delphi" inline>move</syntaxhighlight>}}''' (originated from [[Borland Pascal]]) ''copies'' data from one location in memory to another.
+
The [[Procedure|<syntaxhighlight lang="pascal" inline>procedure</syntaxhighlight>]] '''{{Doc|package=RTL|unit=system|identifier=move|text=<syntaxhighlight lang="delphi" inline>Move</syntaxhighlight>}}''' (originated from [[Borland Pascal]]) ''copies'' data from one location in memory to another.
 
Unlike the name suggests, no data is “lost” at the source.
 
Unlike the name suggests, no data is “lost” at the source.
 
Its signature reads:
 
Its signature reads:
<syntaxhighlight lang="delphi">procedure move(const source; var destination; count: sizeInt)</syntaxhighlight>
+
<syntaxhighlight lang="delphi">procedure Move(const source; var destination; count: sizeInt)</syntaxhighlight>
 
Note that <syntaxhighlight lang="delphi" inline>source</syntaxhighlight> and <syntaxhighlight lang="delphi" inline>destination</syntaxhighlight> do not have a data type, thus specifying an [[Identifier]] ''without'' (necessarily) applying the [[@|address‑operator]] is sufficient.
 
Note that <syntaxhighlight lang="delphi" inline>source</syntaxhighlight> and <syntaxhighlight lang="delphi" inline>destination</syntaxhighlight> do not have a data type, thus specifying an [[Identifier]] ''without'' (necessarily) applying the [[@|address‑operator]] is sufficient.
  

Latest revision as of 14:05, 28 January 2022

The procedure Move (originated from Borland Pascal) copies data from one location in memory to another. Unlike the name suggests, no data is “lost” at the source. Its signature reads:

procedure Move(const source; var destination; count: sizeInt)

Note that source and destination do not have a data type, thus specifying an Identifier without (necessarily) applying the address‑operator is sufficient.

Application

Move can be considered a very “low-level” routine. It was originally developed to copy char values within the same string, but has ever since been (ab‑)used for other purposes, too:

  • If Pascal’s strong data type system imposes (too many) restrictions that need to be circumvented for hardware-close programming and typecasting does not resolve the task’s problem or is too cumbersome.
  • For circumventing Pascal’s restrictions in a very “hacky” nature: E. g. copying a file variable (there are good reasons you cannot simply := to a file).
  • For performance reasons: Copying huge blocks of memory with move could be faster than an equivalent for‑loop. For instance the x86‑64 implementation takes advantage of architecture-specific circumstances.
Note-icon.png

Note: Using move often makes your code very unportable since it often makes assumptions about endianness, internal structures or similar things.

A quick demo should not be missing. Nevertheless, this example is bad in nature. A plain := assignment would have been sufficient. Note, using move possibly prevents the compiler from performing certain optimizations.

program moveDemo(input, output, stdErr);
var
	x, y: integer;
begin
	x := 42;
	move(x, y, sizeOf(x));
	writeLn(y);
end.

See also

  • moveChar0 - move until first chr(0) but at most count char values.
  • copy - creates a copy of data on the heap and returns a pointer to it.