move: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: move
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Procedure|procedure]] **[`Move`](https://www.freepascal.org/docs-html/rtl/system/move.html)** (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: | |
| 2 | + | ||
| 3 | + | ```delphi | |
| 4 | + | procedure Move(const source; var destination; count: sizeInt) | |
| 5 | + | ``` | |
| 6 | + | ||
| 7 | + | Note that `source` and `destination` do not have a data type, thus specifying an [[Identifier]] *without* (necessarily) applying the [[@|address‑operator]] is sufficient. | |
| 8 | + | ||
| 9 | + | ## Application | |
| 10 | + | ||
| 11 | + | `Move` can be considered a very “low-level” routine. It was originally developed to copy `char` values *within* the *same* [[String|string]], but has ever since been (ab‑)used for other purposes, too: | |
| 12 | + | ||
| 13 | + | - If Pascal’s strong data type system imposes (too many) restrictions that need to be circumvented for hardware-close programming and [[Typecast|typecasting]] does not resolve the task’s problem or is too cumbersome. | |
| 14 | + | - For circumventing Pascal’s restrictions in a very “hacky” nature: E. g. *copying* a *[[File|file]] variable* (there are good reasons you cannot simply [[Becomes|:=]] to a `file`). | |
| 15 | + | - For performance reasons: Copying *huge* blocks of memory with `move` *could* be faster than an equivalent [[For|for‑loop]]. For instance the [x86‑64 implementation](https://gitlab.com/freepascal.org/fpc/source/blob/release_3_2_0/rtl/x86_64/x86_64.inc#L75-351) takes advantage of architecture-specific circumstances. | |
| 16 | + | ||
| 17 | + | > **Note:** Using `move` often makes your code very unportable since it often makes assumptions about endianness, internal structures or similar things. | |
| 18 | + | ||
| 19 | + | 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 [[Optimization|optimizations]]. | |
| 20 | + | ||
| 21 | + | ```delphi | |
| 22 | + | program moveDemo(input, output, stdErr); | |
| 23 | + | var | |
| 24 | + | x, y: integer; | |
| 25 | + | begin | |
| 26 | + | x := 42; | |
| 27 | + | move(x, y, sizeOf(x)); | |
| 28 | + | writeLn(y); | |
| 29 | + | end. | |
| 30 | + | ``` | |
| 31 | + | ||
| 32 | + | ## See also | |
| 33 | + | ||
| 34 | + | - [`moveChar0`](https://www.freepascal.org/docs-html/rtl/system/movechar0.html) - `move` until first `chr(0)` but *at most* `count` `char` values. | |
| 35 | + | - [`copy`](https://www.freepascal.org/docs-html/rtl/system/copy.html) - creates a copy of data on the heap and returns a pointer to it. | |
| 36 | + | ||
| 37 | + | --- | |
| 38 | + | *From the Free Pascal wiki, page "move", as it stood on 2022-01-28 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 39 | + | Category: FPC |
