Wiki source for KeyPgPreserve


Show raw source

{{fbdoc item="title" value="PRESERVE"}}----
Used with ##[[KeyPgRedim|Redim]]## to preserve contents will resizing an array

{{fbdoc item="syntax"}}##
[[KeyPgRedim|redim]] **Preserve** //array//(...) [[[KeyPgAs|as]] [[DataType|datatype]]]
##
{{fbdoc item="desc"}}
Used with ##[[KeyPgRedim|Redim]]## so that when an array is resized, data is not reset but is preserved. This means when the array is enlarged that only new data is reset, while the old data remains the same (but not necessarily at the same absolute addresses in memory).

**NOTE**: ##**Redim** [[KeyPgPreserve|Preserve]]## may not work as expected in all cases:
##[[KeyPgPreserve|Preserve]]##'s current behavior is to keep the original data contiguous in memory, and only expand or truncate the size of the memory (if resizing is not possible, the whole original data block is first shifted to another memory location).
Its behavior (with a single dimension) is well-defined only when the upper bound is changed. If the lower bound is changed, the current result is that the data is in effect shifted to start at the new lower bound.
If there are multiple dimensions, only the upper bound of only the first dimension may be changed safely. If the first dimension is reduced, the existing mappable data may be lost. If lower-order dimensions are resized at all, the effects can be hard to predict (because multidimensional arrays are stored in row-major order : values differing only in the last index are contiguous).

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/array/preserve.bas"}}%%(freebasic)
redim array(1 to 3) as integer
dim i as integer

array(1) = 10
array(2) = 5
array(3) = 8

redim preserve array(1 to 10)

for i = 1 to 10
print "array("; i; ") = "; array(i)
next
%%

{{fbdoc item="diff"}}
- ##**Preserve**## wasn't supported until PDS 7.1

{{fbdoc item="see"}}
- ##[[KeyPgDim|Dim]]##
- ##[[KeyPgLbound|Lbound]]##
- ##[[KeyPgRedim|Redim]]##
- ##[[KeyPgUbound|Ubound]]##

{{fbdoc item="back" value="CatPgArray|Array Functions"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode