Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
REDIM takes more resources than DIM?
#35
Some background context regarding arrays:

QuickBasic has two separate kinds of arrays, static and dynamic. Static arrays have their bounds known at compile time, and so the compiler can emit more efficient code. Dynamic arrays are the opposite, their bounds are not known until the program reaches the statements at runtime.

When you write DIM a(x), the compiler must create a dynamic array as the value of x is not known until the program runs. When you write DIM a(3), the compiler can be more efficient and create a static array.

Here is QB 7.1 creating and accessing a dynamic array (apologies for screenshots over text, it is awkward to get listings out of codeview):


[Image: Screenshot-20240723-224753.png]
First we must call the B$DDIM helper routine with arguments specifying lower (0) and upper (the value of x) bounds, element size (4) and number of dimensions (1). When we access an element, we first read the array lower bound from memory, add that to the index we want then finally access the array.

Whereas here is creating and accessing a static array:

[Image: Screenshot-20240723-225844.png]

Since the compiler knows the size of the array, space is already reserved for it and no initialisation is required. This is why you can't redim a static array: it has its fixed spot in memory. Because the lower bound is also known at compile time, the memory address of each element can be emitted directly.

So static arrays are more efficient, but their efficiency relies on them being at a fixed location in memory with a fixed size. Whereas dynamic arrays can be resized, but move around in memory and need extra memory accesses to calculate offsets.

QB64 does not make the same degree of optimisation regarding static arrays, but still enforces the restriction on resizing them. I suppose in the future it could optimise this, but there are probably smarter ways to do it.

PS This is one time CONST makes a difference. CONST x = 3: DIM a(x) gives a static array, without the CONST it would be a dynamic array.
Reply


Messages In This Thread
REDIM takes more resources than DIM? - by bplus - 07-17-2024, 12:21 AM
RE: REDIM takes more resources than DIM? - by luke - 07-23-2024, 01:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Preserving multi-dim arrays Pete 5 416 12-19-2025, 03:17 PM
Last Post: Dimster
  packaging resources tool TempodiBasic 6 580 09-22-2025, 11:16 PM
Last Post: TempodiBasic
  DIM AT -- feature request Jack 2 709 08-15-2023, 12:15 AM
Last Post: grymmjack
  REDIM, TYPE, and STRING Woes TerryRitchie 16 3,139 04-13-2023, 05:17 AM
Last Post: DSMan195276
  suggestion: initialize array values within the DIM statement bobalooie 19 3,346 12-22-2022, 06:46 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: