Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
(07-17-2024, 12:49 PM)aurel Wrote: Quote:because a dynamic array only exists while the program is running
excuse me @Kernel but that is kind of funny
of course that array exist in memory when program running Yes, it's not so easy to make something understandable this.
The lifespan of static variables or arrays corresponds to that of the program module in which they were defined. They cannot really be deleted. With Erase, all array elements are only initialized with zero.
The memory space of dynamic arrays, on the other hand, is only acquired at runtime when a declaration is made in the program. The Erase command enables the occupied memory space to be released, when the array is no longer needed.
Posts: 34
Threads: 4
Joined: Apr 2022
Reputation:
14
(07-17-2024, 09:16 AM)SMcNeill Wrote: May also be an issue with having to reset array values to 0.
TYPE foo
x AS STRING
y AS LONG
END TYPE
REDIM whatever(1000) AS foo
I seem to remember that the above doesn't always initialize arrays to 0, so you may need to do so manually for that, leading to extra run time and such. This was a bug that will be fixed in the next release.
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
Quote:If its completely erased, then why would ReDim not also be able to resurrect a new name for the array?
@Dimster, it's OK! It took me a while to understand that too; I hope so.
Deleting with Erase only refers to the memory location of a declared array. Example: twoDimfield. If you now enter: ReDim threeDimfield, what is that supposed to do? The array threeDimfield wasn't even declared.
Code: (Select All)
'Zweidimensionales Feld mit Redim loeschen - 4. Feb. 2024
'Beispiel fuer Anwendung von Erase. Wird Erase auf ein dynamische
'Feld angewendet, existiert das Feld nicht mehr. Bei einem statischen
'Feld wird dieses mit Nullen gefuellt
$Console:Only
Option _Explicit
Option Base 1
Dim As Integer zeilenDim, spaltenDim
Dim As Integer i, j, z, fehlerNummer
On Error GoTo ausserhalbIndex
Locate 2, 2
Input "Feldimension Zeilen : ", zeilenDim
Locate 3, 2
Input "Felddimension Spalten: ", spaltenDim
'Feld mit Vorgaben initialisieren
Dim As Integer zweiDimfeld(zeilenDim, spaltenDim)
Locate CsrLin + 2, 2
z = 1
For i = 1 To zeilenDim
For j = 1 To spaltenDim
zweiDimfeld(i, j) = z
Print Using "## "; zweiDimfeld(i, j),
z = z + 1
Next
Print: Locate , 2
Next
'Vor Neudimensionierung Speicher freigeben. Ist bei Anwendung
'von REDIM nicht noetig, da dieser ERASE + DIM zusammenfasst - S.188
Locate CsrLin + 2, 2
Print "Redim: ";: Print "Wert von z = "; z
ReDim zweiDimfeld(zeilenDim, spaltenDim)
Locate CsrLin + 2, 2
For i = 1 To zeilenDim
For j = 1 To spaltenDim
zweiDimfeld(i, j) = z
Print Using "## "; zweiDimfeld(i, j),
z = z + 1
Next
Print: Locate , 2
Next
'Ohne Zeile, Spalte bleibt der alte Wert erhalten
ReDim zweiDimfeld(zeilenDim, spaltenDim)
Print: Print "Wert von z neu setzen."
Input "z: ", z
Locate CsrLin + 2, 2
For i = 1 To zeilenDim
For j = 1 To spaltenDim
zweiDimfeld(i, j) = z
Print Using "## "; zweiDimfeld(i, j),
z = z + 1
Next
Print: Locate , 2
Next
Locate CsrLin + 1, 2
Print "Anwendung von ERASE. Feld neu belegen:"
Erase zweiDimfeld
Locate CsrLin + 1, 2
Print "Nach ERASE existiert das Feld nicht mehr."
Locate CsrLin + 1, 2
z = 0
For i = 1 To zeilenDim
For j = 1 To spaltenDim
zweiDimfeld(i, j) = z
Print Using "## "; zweiDimfeld(i, j),
z = z + 1
Next
Print: Locate , 2
Next
'Locate CsrLin + 3, 2
'Error fehlerNummer
'Warum kommt hier eine Fehlermeldung?
ausserhalbIndex:
'If Err = 9 Then Error Err Else Print "Kein Zugriff! Feld existiert nicht mehr."
'Resume Next
End
Posts: 301
Threads: 16
Joined: Apr 2022
Reputation:
51
in some other languages, static arrays are allocated in stack space while dynamic in heap space, so there might be some performance hit in exchange for more available space
no idea what QB64PE resolves it to, though
Posts: 3,965
Threads: 176
Joined: Apr 2022
Reputation:
219
(07-17-2024, 01:36 PM)luke Wrote: (07-17-2024, 09:16 AM)SMcNeill Wrote: May also be an issue with having to reset array values to 0.
TYPE foo
x AS STRING
y AS LONG
END TYPE
REDIM whatever(1000) AS foo
I seem to remember that the above doesn't always initialize arrays to 0, so you may need to do so manually for that, leading to extra run time and such. This was a bug that will be fixed in the next release.
I seem to recall it being fixed once at least for number in UDT's but varaible length strings messed things up.
@Luke didn't you once say, I forget which forum, it doesn't matter if DIM everything or REDIM everything.
Because from that I experimented with REDIM for everything, no problems! (until the extra 2 characters became tiresome and REDIM looked odd for plain variables).
b = b + ...
Posts: 2,171
Threads: 222
Joined: Apr 2022
Reputation:
103
(07-17-2024, 02:10 PM)vince Wrote: in some other languages, static arrays are allocated in stack space while dynamic in heap space, so there might be some performance hit in exchange for more available space
no idea what QB64PE resolves it to, though
I swear I can recall the need to make all my arrays in QuickBASIC to get my programs to run. STATIC arrays used up too much memory, probably from the stack space. You could use the CLEAR command with parameters to set stack space and memory allocation ratios, to some degree. I always needed more stack space. Life was so much harder then, 20 years ago...
Pete
Posts: 1,002
Threads: 50
Joined: May 2022
Reputation:
27
(07-17-2024, 02:10 PM)vince Wrote: in some other languages, static arrays are allocated in stack space while dynamic in heap space, so there might be some performance hit in exchange for more available space
no idea what QB64PE resolves it to, though "quickbasic" had listed two books for download: https://qb64phoenix.com/forum/showthread...472&page=2
I have Book 2: QBasic programmers reference in German. The book is very good! What is discussed here starts on page 165, Erase. It also covers what is put on the heap and what is not, and so on.
The book takes into account the then newest and last version of QBasic 1.1, which is newer than QuickBasic 4.5. - QBasic and QuickBasic are the same, but you cannot create exe files with QBasic.
Posts: 303
Threads: 10
Joined: Apr 2022
Reputation:
44
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.
Posts: 2,171
Threads: 222
Joined: Apr 2022
Reputation:
103
But it's always hard to see what luke is getting at, because he's forever joking around.
Pete
Posts: 296
Threads: 10
Joined: Apr 2022
Reputation:
6
My point was if we need to increase array size we should use REDIM ..right ?
so
DIM arr(100)
REDIM arr(1000)
that is only useful if we don't know what size we need..right?
so if we have loop ..for example which require more space then REDIM
in my own programs i never use REDIM i use fixed size.
but that is me ..
|