You may wonder, why all these different names for arrays? Syntactically it’s all very similar, the only difference is whether the size of a dimension is fixed or not, or is it?
The truth is that in most cases it doesn't really matter whether you use a static, dynamic or jagged array. Therefore the simple guideline is to always prefer to leave the size of all dimensions unspecified. It’s simple, easy to remember and easy to use. It also gives you the most flexibility, as the size of each dimension will grow as needed. Technically speaking, this means that you would favor dynamic arrays over static arrays, and jagged arrays over multidimensional dynamic arrays.
If you should always prefer to leave the size of all dimensions unspecified, then what is the point of letting you specify the size of a dimension? While it’s true that static, dynamic and jagged arrays are syntactically very similar, the implementation strategies are very different. The technical implementation differences is of very little or no concern in most cases, but in some very specific cases it does matter. This section will try to describe these technical implementation differences, and when the differences matter.
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.
If you're not sure whether you need C style array compatibility, then there's no reason to use a static array. You only need to use a static array if you really need binary compatibility with a C style static array. For example, if you're declaring a struct that will be used together with an external DLL function, and the struct member is declared as a static array in C. Again, unless you're directly translating a C struct, there's no need to use a static array member. DataFlex fully supports dynamic array members in a struct. Static arrays are only needed when you require binary compatibility with C style arrays.
Rectangular multidimensional arrays are useful when you need compatibility with C style multidimensional arrays. Jagged arrays are not binary compatible with C, so if you really need binary compatibility with C style multidimensional arrays, you must use rectangular multidimensional arrays instead of jagged arrays.
If you know that the size of each dimension, except possibly the first one, is always fixed, then it might be more convenient to use rectangular multidimensional arrays. Otherwise it might be easier to use a jagged array instead.
Using a jagged array instead of a rectangular multidimensional array has the advantage that you can vary the number of columns for each row, and that each row will by default have 0 columns. This can lower memory usage if there are many rows where not all columns are used. The downside is that since each row will by default have 0 columns, before getting the value of a particular column, you must first establish that the column you want to access exist, or a run-time error may occur. Thus it may in some cases be more convenient and/or easier to use a rectangular array.
If you're not sure whether your array will be rectangular in nature, then use a jagged array. Jagged arrays always gives you the most flexibility, all dimensions are dynamic and will automatically resize as needed.
Static array members of a struct take up the exact space needed for the array directly embedded in the struct. For example, a struct type containing a member Integer, 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.
Dynamic array members of a struct always only take up the size of a pointer (normally 4 bytes). No matter how many elements there are in the array, the number of bytes used by that particular array member in the struct is always the same. For example, if you have a struct containing only one member which is a dynamic array, the size of that struct type is always 4 bytes.
It's said that 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.
In most cases this difference is of little or no importance. The syntax for accessing static/dynamic array members of a struct is the same. The structural difference may be important for binary compatibility with C style arrays though. Dynamic arrays, as opposed to static arrays, can also be used to form indirect-recursive struct declarations.
Jagged arrays are technically and structurally just dynamic single-dimensional arrays of dynamic single-dimensional array type. A jagged array is simply put, an array of array type.
Syntactically, jagged arrays and multidimensional arrays are very similar. Jagged arrays are just as easy to use as rectangular multidimensional arrays.
Since jagged arrays are structurally dynamic arrays, jagged array members of a struct are only indirectly embedded in the struct. Jagged arrays can therefore be used to form indirect-recursive struct declarations.