Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
REDIM takes more resources than DIM?
#21
@aurel

DIM arr(100)
REDIM arr(1000)

That would cause a duplicate definition error.

Edit: To be clearer, you can do...

REDIM a(100)
REDIM a(1000)

or...

ReDim a(100)
Erase a
Dim a(1000)

but you cannot redefine a static array, even if you try to erase it...

Dim a(100)
Erase a <------ WON'T WORK.
Dim a(1000)
Reply
#22
(07-17-2024, 06:59 PM)DSMan195276 Wrote: QB64 currently allocates both regular and dynamic arrays at runtime and on the heap (never on the stack). As far as QB64 programs are concerned this distinction doesn't matter, but it does mean that if you use `ReDim` to initially declare an array (and do not resize it later) then it's functionally no different from one declared via `Dim`. I'm pretty confident this is what Luke was getting at if/when he mentioned that you could replace `Dim` with `ReDim` with no penalty.
There are problems here, but why? Personally, I think it is wrong to declare an array with ReDim... that is confusing. For me, the clear line is lost.

Code: (Select All)

Option Base 1

ReDim As Integer feld(5, 5)

feld(1, 2) = 3
Print feld(1, 2)

ReDim feld(4, 7)

feld(3, 5) = 12
Print feld(3, 5)

Print: Print
Dim As Integer feld2(6, 8)

feld2(4, 1) = 10
Print feld2(4, 1)

ReDim feld2(9, 9)
feld2(8, 9) = 22

Print feld2(8, 9)

[Image: Re-Dim-Co2024-07-17.jpg]
Reply
#23
(07-17-2024, 08:12 PM)Kernelpanic Wrote: There are problems here, but why? Personally, I think it is wrong to declare an array with ReDim... that is confusing. For me, the clear line is lost.
I think you're getting too caught up in the name `ReDim`, it's potentially a badly chosen command name. `ReDim` does not pair with `Dim`, you can't use `Dim` initially and then use `ReDim` afterwards. Rather, `Dim` and `ReDim` declare different types of arrays, static vs. dynamic. Only dynamic arrays declared using `ReDim` can be subsequently resized by another `ReDim`.

This detail is mentioned on the Wiki page for `ReDim`, though it's about halfway down:

Quote:REDIM cannot change $STATIC arrays created with a DIM statement unless the $DYNAMIC Metacommand is used!
To create a dynamic array use the $DYNAMIC metacommand or use REDIM rather than DIM when first creating the array.
(Note that it does work like you're thinking if you use `$Dynamic`, but IMO you should forget that exists and just use `ReDim`)
Reply
#24
Quote:I think you're getting too caught up in the name `ReDim`, it's potentially a badly chosen command name. `ReDim` does not pair with `Dim`, you can't use `Dim` initially and then use `ReDim` afterwards. Rather, `Dim` and `ReDim` declare different types of arrays, static vs. dynamic. Only dynamic arrays declared using `ReDim` can be subsequently resized by another `ReDim`.
I think I know what you mean. The field2() declared with Dim is static, so of course it can't be changed. That's right, that's what it says in the reference. I wasn't paying attention. I'll take a closer look tomorrow.
Reply
#25
My Av-ee-tar requests a new QB64 keyword for this...

_DIMITALL
Reply
#26
Dynamic arrays are always created when no constant value is specified in the DIM statement - but also arrays declared as COMMON are declared dynamically.

OK, that's how it works. But I stick to my opinion that I have already expressed: Personally, I think it is wrong to declare an array with ReDim... that is confusing. For me, the clear line is lost.

Code: (Select All)

Option Base 1

ReDim As Integer feld(5, 5)

feld(1, 2) = 3
Print feld(1, 2)

ReDim feld(4, 7)

feld(3, 5) = 12
Print feld(3, 5)

ReDim feld(5, 5)

feld(2, 2) = 7
Print feld(2, 2)

'---------------------------------------

Dim As Integer a, b

'Jetzt wird feld2() dynamisch deklariert
'Now the array is created dynamically.
a = 5
b = 7
Dim As Integer feld2(a, b)

feld2(2, 2) = 8
Print feld2(2, 2)

ReDim feld2(b, a)

feld2(4, 2) = 10
Print feld2(4, 2)
Reply
#27
Cool, This is a good point, and something I can't quite off-hand come up with a reason why a variable can be used to avoid a numerically assigned array duplicate definition error...

Code: (Select All)

a = 5
b = 7
' This first half assigns a variable to each array and works.
Dim x(a)
For i = 1 To a: Print i; x(i): Next
Print "-----------------"
ReDim x(b)
For i = 1 To b: Print i; x(i): Next
Print "-----------------"

' This second half assigns numbers to the arrays, and errors out at line 15.
Dim y(5)
For i = 1 To a: Print i; x(i): Next
Print "-----------------"
ReDim y(7)
For i = 1 To b: Print i; x(i): Next

Pete
Reply
#28
(07-18-2024, 07:51 PM)Pete Wrote: Cool, This is a good point, and something I can't quite off-hand come up with a reason why a variable can be used to avoid a numerically assigned array duplicate definition error...

Code: (Select All)

a = 5
b = 7
' This first half assigns a variable to each array and works.
Dim x(a)
For i = 1 To a: Print i; x(i): Next
Print "-----------------"
ReDim x(b)
For i = 1 To b: Print i; x(i): Next
Print "-----------------"

' This second half assigns numbers to the arrays, and errors out at line 15.
Dim y(5)
For i = 1 To a: Print i; x(i): Next
Print "-----------------"
ReDim y(7)
For i = 1 To b: Print i; x(i): Next
I don't understand what you're trying to say. It's clear that ReDim y(7) produces an error. Dim y(5) doesn't specify a constant value.
That's how it works.

Code: (Select All)

$Console:Only

a = 5
b = 7

' This first half assigns a variable to each array and works.
Dim x(a)
For i = 1 To a: Print i; x(i): Next
Print "-----------------"
ReDim x(b)
For i = 1 To b: Print i; x(i): Next
Print "-----------------"

' This second half assigns numbers to the arrays, and errors out at line 15.
'That's how it works.
y = 5
Dim z(y)
For i = 1 To a: Print i; z(i): Next
Print "-----------------"
ReDim z(b)
For i = 1 To b: Print i; z(i): Next
Reply
#29
(07-22-2024, 02:34 PM)Kernelpanic Wrote:
(07-18-2024, 07:51 PM)Pete Wrote: Cool, This is a good point, and something I can't quite off-hand come up with a reason why a variable can be used to avoid a numerically assigned array duplicate definition error...
I don't understand what you're trying to say. It's clear that ReDim y(7) produces an error. Dim y(5) doesn't specify a constant value.
That's how it works.

Pete's point is a good catch, which is that the `ReDim x(b)` line should produce the same error as the `ReDim y(7)` line. I assume it's just a bug, though I guess the QBasic behavior should be checked before saying that for sure.

Usage of a variable for the bounds is different from a constant because the array has to be allocated/initialized at the position of the `Dim` statement, rather than at the start of the program or SUB/FUNCTION. This is similar to how `ReDim` works, the allocation takes place at the point of the `ReDim`, and it seems like the flag indicating the array is static never gets set when allocated this way. If the flag is missing then you're allowed to do a `ReDim`, so you don't get an error.
Reply
#30
Quote:Pete's point is a good catch, which is that the `ReDim x(b)` line should produce the same error as the `ReDim y(7)` line. I assume it's just a bug, though I guess the QBasic behavior should be checked before saying that for sure.

Usage of a variable for the bounds is different from a constant because the array has to be allocated/initialized at the position of the `Dim` statement, rather than at the start of the program or SUB/FUNCTION. This is similar to how `ReDim` works, the allocation takes place at the point of the `ReDim`, and it seems like the flag indicating the array is static never gets set when allocated this way. If the flag is missing then you're allowed to do a `ReDim`, so you don't get an error.
I don't know what happens internally, but the way I did it is described in the QBasic Programmer's Reference. And it works.

PS: And in the Quick Basic 4.5 manual; Dim and ReDim. Pretty extensive, and not that easy.
Reply




Users browsing this thread: 1 Guest(s)