Data Access Worldwide Knowledge Base

Article ID 2103
Article Title INFO: The ResizeArray Function and Arrays
Article URL http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=2103
KBase Category VDF11
Date Created 04/04/2005
Last Edit Date 04/04/2005


Article Text
ResizeArray() was basically created for two main purposes: clearing an array (erasing all elements), and for preparing an array for many insertions (quickly and efficiently expanding to a specified size).

The original intent of ResizeArray() was to accomodate the two operations of clearing/shrinking the array(freeing up memory), and quickly expanding the array to a specified size (pre-allocating memory).

ResizeArray is implemented as a function mostly for consistency. All other type manipulation routines are implemented as functions. As you've probably noticed, there's often an overlap of commands and functions (especially for older types), such as the Length command and the Length function. Over time, the command variants of these operations have been phased out and made obsolete in favor of their respective function variants.

The ResizeArray function doesn't really resize the specified array, but rather makes a copy of it. The following sentence should have been in the help: "The array is not resized in-place, but rather a copy is made, resized and assigned to the destination variable"

It's actually not the only operation that works this way. DateSetDay(), DateSetHour() etc. work the same way. They're not actually setting the day/hour portion of the specified variable, but rather making a copy of the passed variable and setting the respective day/hour portion of the copy and assigning it to the destination variable. In other words, it works just like ResizeArray().



Usage:
Only dynamic dimensions can be resized. A dynamic single dimensional array can be resized, but a static single dimensional array cannot be resized.

The first (most significant) dimension of a regular multidimensional array can be either dynamic or static, whereas all other dimensions must be static. If the first dimension is dynamic, it's called a dynamic multidimensional array, otherwise it's called a static multidimensional array.

This is perhaps best illustrated using a two-dimensional table. A regular database table is a good example of a dynamic multidimensional array. The first dimension represents rows, and the second dimension represents columns. The table consists of an undefined number of rows with a fixed number of columns. In other words, the row dimension is dynamic, while the column dimension is static.

This means that only the first dimension of a dynamic multidimensional array can be resized.

A jagged array is like a dynamic multidimensional array on steroids, where all dimensions are dynamic. Each row may have different number of columns. Which means that all dimensions of a jagged array can be resized.



Syntax:
The built-in functions -- like ResizeArray() -- are not the same as global functions; they're only available as expressions. So in order to use ResizeArray(), you would have to use move:

Integer[] iValues
Integer iCount

Move (ResizeArray(iValues, 100)) to iValues // creates 100 array elements

and not

Get ResizeArray iValues 100 to iValues



Future Plans:
We have plans to add more array manipulation routines in the revision after Visual DataFlex 11.0. The ability to efficently extract and copy a specified range of an array is among them.



Arrays:
Jagged arrays are not a kind of regular multidimensional dynamic arrays. Consequently, jagged arrays are not a subset of multidimensional arrays. Jagged arrays are actually structurally very different from regular multidimensional arrays. Syntactically they're very similar, but that's the only similarity. That's why it's important to not confuse the two, and to separate them.

Dynamic arrays and jagged arrays will always resize automatically when necessary, and they give you the most flexibility and they are the easiest to use. This is why it's expected that dynamic arrays and jagged arrays will be used the most.

Static arrays are useful when you need compatibility with C-style arrays, and when you need to allocate a fixed-size array directly embedded in a struct, again mostly for C compatibility.

Static array members of a struct take up the exact space needed for the array directly embedded in the struct. In other words, a struct containing a member Integer[5] will take up (5*SizeOfType(Integer)) bytes directly in the struct. So, if you have a struct containing only one member which is a static array, the size of that struct type is the size of that static array.

However, dynamic array members of a struct always only take up the size of a pointer (4 bytes). No matter how many elements there are in the array, the size of that particular array member in the struct is always 4 bytes. In other words, if you have a struct containing only one member which is a dynamic array, the size of that struct type is always 4 bytes.

Static array members of a struct are directly embedded in the struct, whereas dynamic array members of a struct are only indirectly embedded in the struct.

Jagged arrays are technically and structurally just dynamic single-dimensional arrays of dynamic single-dimensional array type. In other words, it's an array of array.

Since jagged arrays are arrays of arrays, they are not compatible with C-style arrays. But for most developers, this is of no concern. But if you really need binary compatibility with C-style multidimensional arrays, then you must use regular multidimensional arrays instead of jagged arrays.

Syntactically, jagged arrays and multidimensional arrays are very similar. Jagged arrays are just as easy (or maybe even easier) to use as multidimensional arrays, which is why most developers will just use jagged arrays. That way you won't even have to think about whether the particular dimension can be resized, as all dimensions of a jagged array can be resized.






Contributed By:
Sonny Falk
Company: Data Access Worldwide
Web Site: http://www.dataaccess.com


Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2010 Data Access Corporation. All rights reserved.

The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.