Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CLEAR command
#1
Hi, I need an explanation about the command "CLEAR".

DIM a% locates a part of memory for the variable a%, that is empty.

a%=1 give to the variable a% the value 1.

What does CLEAR? Does it makes a% empty again, or does it deletes the memory located for a%, virtually "deleting" the command DIM a%?
Reply
#2
(02-02-2024, 10:13 PM)bartok Wrote: Hi, I need an explanation about the command "CLEAR".

DIM a% locates a part of memory for the variable a%, that is empty.

a%=1 give to the variable a% the value 1.

What does CLEAR? Does it makes a% empty again, or does it deletes the memory located for a%, virtually "deleting" the command DIM a%?
CLEAR erases the values within variables. The variables still exist they are just nulled or set to zero.

Dynamic arrays (REDIM) are completely removed however and will need to be redimensioned after a CLEAR statement. The code below shows the different behaviors.

Code: (Select All)
OPTION _EXPLICIT

DIM a%
DIM s$

REDIM b%(20)

a% = 1
s$ = "Test"
b%(20) = 2
PRINT a%
PRINT s$
PRINT b%(20)
CLEAR
PRINT a% ' a reset to 0
PRINT s$ ' reset to null
'PRINT b%(20) ' if you activate this line you'll get an error
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#3
(02-03-2024, 02:53 AM)TerryRitchie Wrote:
(02-02-2024, 10:13 PM)bartok Wrote: Hi, I need an explanation about the command "CLEAR".

DIM a% locates a part of memory for the variable a%, that is empty.

a%=1 give to the variable a% the value 1.

What does CLEAR? Does it makes a% empty again, or does it deletes the memory located for a%, virtually "deleting" the command DIM a%?
CLEAR erases the values within variables. The variables still exist they are just nulled or set to zero.

Dynamic arrays (REDIM) are completely removed however and will need to be redimensioned after a CLEAR statement. The code below shows the different behaviors.

Code: (Select All)
OPTION _EXPLICIT

DIM a%
DIM s$

REDIM b%(20)

a% = 1
s$ = "Test"
b%(20) = 2
PRINT a%
PRINT s$
PRINT b%(20)
CLEAR
PRINT a% ' a reset to 0
PRINT s$ ' reset to null
'PRINT b%(20) ' if you activate this line you'll get an error
Hi TerryRitchie. I take this opportunity in order to thank you for your tutorials that I used in 2020 in order to start learning QB64. I see that there are new lessons.

However, I was on the verge to ask you how do you know that CLEAR command doesn't remove variables set with DIM too, because for example:
PRINT a% 'with variable a% no set with DIM.
brings to the same result of:
DIM a%
a%=2
CLEAR
PRINT a%
This example make possible to think that CLEAR command might as well remove variable a%.

However:
DIM b%(30) 'like that the program works.
'REDIM b%(30) 'like that the program get an error.
b%(20) = 2
PRINT b%(20)
CLEAR
PRINT b%(20)

So I think that it could respond to my doubt.

But why CLEAR erase dynamic arrays and not set them to their initial dimension?

I make these questions because I have a program that has to be navigable by means of DO loops, and I have to chose what variables and arrays I can avoid to put into the first level of the DO loops, leaving them only on the start of the program, outside the DO loops, in order to don't repete uselessly their setting.
Reply
#4
I dony' understand. Look at this code:

Code: (Select All)

'DIM SHARED b%(30) 'if activated, the program doesn't work.
REDIM SHARED b%(30) 'if activated, the program works.
'if both deactivated, the program doesn't work.
CLEAR
FOR i% = 1 TO 40
    REDIM _PRESERVE b%(i%)
    b%(i%) = i%
NEXT i%

CALL ciao

SUB ciao
    PRINT b%(3)
END SUB

So, if CLEAR completely remove b%(), so that it doesn't exist anymore, why it is however needed REDIM SHARED b%(30) before CLEAR? It seems that CLEAR completely remove b%(), without removing the information that it is SHARED. But how is it possible that an inexistent array could be SHARED?
Reply
#5
(02-03-2024, 02:48 PM)bartok Wrote: I dony' understand. Look at this code:

Code: (Select All)

'DIM SHARED b%(30) 'if activated, the program doesn't work.
REDIM SHARED b%(30) 'if activated, the program works.
'if both deactivated, the program doesn't work.
CLEAR
FOR i% = 1 TO 40
    REDIM _PRESERVE b%(i%)
    b%(i%) = i%
NEXT i%

CALL ciao

SUB ciao
    PRINT b%(3)
END SUB

So, if CLEAR completely remove b%(), so that it doesn't exist anymore, why it is however needed REDIM SHARED b%(30) before CLEAR? It seems that CLEAR completely remove b%(), without removing the information that it is SHARED. But how is it possible that an inexistent array could be SHARED?
In this example a dynamic array is created ( b%() ) and then immediately removed with CLEAR. However, inside the loop the dynamic array is once again created when i% equals 1 and then increased in size when i% is greater than 1. See below. Your code actually creates b%() twice, not once to be reused.

Code: (Select All)
' here the code is creating a dynamic array of 31 indexes all with a value of 0

REDIM SHARED b%(30) 'if activated, the program works.

' CLEAR will now remove this dynamic array

CLEAR

' this loop will create a new dynamic array at first and then populate it

FOR i% = 1 TO 40

    ' when i% = 1 the code creates a new instance of b%() with a dimension of 1 ( b%(1) )

    ' when i% > 1 the code resizes the existing array ( b%(i%) ) and preserves the data previously inserted

    REDIM _PRESERVE b%(i%)
    b%(i%) = i%
NEXT i%

CALL ciao

SUB ciao
    PRINT b%(3)
END SUB
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#6
(02-03-2024, 09:52 AM)bartok Wrote:
(02-03-2024, 02:53 AM)TerryRitchie Wrote:
(02-02-2024, 10:13 PM)bartok Wrote: Hi, I need an explanation about the command "CLEAR".

DIM a% locates a part of memory for the variable a%, that is empty.

a%=1 give to the variable a% the value 1.

What does CLEAR? Does it makes a% empty again, or does it deletes the memory located for a%, virtually "deleting" the command DIM a%?
CLEAR erases the values within variables. The variables still exist they are just nulled or set to zero.

Dynamic arrays (REDIM) are completely removed however and will need to be redimensioned after a CLEAR statement. The code below shows the different behaviors.

Code: (Select All)
OPTION _EXPLICIT

DIM a%
DIM s$

REDIM b%(20)

a% = 1
s$ = "Test"
b%(20) = 2
PRINT a%
PRINT s$
PRINT b%(20)
CLEAR
PRINT a% ' a reset to 0
PRINT s$ ' reset to null
'PRINT b%(20) ' if you activate this line you'll get an error
Hi TerryRitchie. I take this opportunity in order to thank you for your tutorials that I used in 2020 in order to start learning QB64. I see that there are new lessons.

However, I was on the verge to ask you how do you know that CLEAR command doesn't remove variables set with DIM too, because for example:
PRINT a% 'with variable a% no set with DIM.
brings to the same result of:
DIM a%
a%=2
CLEAR
PRINT a%
This example make possible to think that CLEAR command might as well remove variable a%.

However:
DIM b%(30) 'like that the program works.
'REDIM b%(30) 'like that the program get an error.
b%(20) = 2
PRINT b%(20)
CLEAR
PRINT b%(20)

So I think that it could respond to my doubt.

But why CLEAR erase dynamic arrays and not set them to their initial dimension?

I make these questions because I have a program that has to be navigable by means of DO loops, and I have to chose what variables and arrays I can avoid to put into the first level of the DO loops, leaving them only on the start of the program, outside the DO loops, in order to don't repete uselessly their setting.
If a% were completely removed OPTION _EXPLICIT would flag the subsequent use of a% after the CLEAR statement as not being DIMensioned.

I'm glad you found the tutorials useful. If you used them in 2020 then you were probably still using the old tutorial site. A lot of lessons, new code, and changes have been made since then. There were also quite a few areas in the tutorial that needed clarification for better understanding. When you get time let me know what you think of the new tutorial site and with any suggestions you may have.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#7
May I ask you @bartok why you are using the CLEAR command?

The CLEAR command made sense back in the BASIC-80 days:

CLEAR stringspace, memory, stacksize

Then GWBASIC dropped the need to allocate string space (it was actually BASIC-80 version 5):

CLEAR , memory, stacksize  (this is why this is the only BASIC command that has a parameter list that starts with a comma)

Then QuickBasic dropped the need for memory allocation:

CLEAR , , stacksize

and now QB64(PE) has dropped the parameters all together:

CLEAR , ignored, ignored

The CLEAR statement is included to maintain compatibility with GWBASIC and QuickBasic. The other "features" of CLEAR could/should be handled using QB64PE code instead.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#8
(02-03-2024, 05:22 PM)TerryRitchie Wrote: May I ask you @bartok why you are using the CLEAR command?

The CLEAR command made sense back in the BASIC-80 days:

CLEAR stringspace, memory, stacksize

Then GWBASIC dropped the need to allocate string space (it was actually BASIC-80 version 5):

CLEAR , memory, stacksize  (this is why this is the only BASIC command that has a parameter list that starts with a comma)

Then QuickBasic dropped the need for memory allocation:

CLEAR , , stacksize

and now QB64(PE) has dropped the parameters all together:

CLEAR , ignored, ignored

The CLEAR statement is included to maintain compatibility with GWBASIC and QuickBasic. The other "features" of CLEAR could/should be handled using QB64PE code instead.
I use CLEAR because I have a program in which there a lot of arrays that become all populated. But I want that the user is be able to restart the program itself without closing and re-executing it. So, there is the first level of DO loops that has this purpouse: if the user press a defined key, the program leaves all the DO loops, except the DO loop of the first level and it returns to the beginning of the program. But in this case, in order to not have errors during the use of the program, I need to have all the variables and arrays CLEARed.
Reply
#9
(02-03-2024, 04:29 PM)TerryRitchie Wrote:
(02-03-2024, 02:48 PM)bartok Wrote: I dony' understand. Look at this code:

Code: (Select All)

'DIM SHARED b%(30) 'if activated, the program doesn't work.
REDIM SHARED b%(30) 'if activated, the program works.
'if both deactivated, the program doesn't work.
CLEAR
FOR i% = 1 TO 40
    REDIM _PRESERVE b%(i%)
    b%(i%) = i%
NEXT i%

CALL ciao

SUB ciao
    PRINT b%(3)
END SUB

So, if CLEAR completely remove b%(), so that it doesn't exist anymore, why it is however needed REDIM SHARED b%(30) before CLEAR? It seems that CLEAR completely remove b%(), without removing the information that it is SHARED. But how is it possible that an inexistent array could be SHARED?
In this example a dynamic array is created ( b%() ) and then immediately removed with CLEAR. However, inside the loop the dynamic array is once again created when i% equals 1 and then increased in size when i% is greater than 1. See below. Your code actually creates b%() twice, not once to be reused.

Code: (Select All)
' here the code is creating a dynamic array of 31 indexes all with a value of 0

REDIM SHARED b%(30) 'if activated, the program works.

' CLEAR will now remove this dynamic array

CLEAR

' this loop will create a new dynamic array at first and then populate it

FOR i% = 1 TO 40

    ' when i% = 1 the code creates a new instance of b%() with a dimension of 1 ( b%(1) )

    ' when i% > 1 the code resizes the existing array ( b%(i%) ) and preserves the data previously inserted

    REDIM _PRESERVE b%(i%)
    b%(i%) = i%
NEXT i%

CALL ciao

SUB ciao
    PRINT b%(3)
END SUB
About that, I undestand your point: I have created b%() twice. What I don't understand is the reason why "CALL ciao" works only if "REDIM SHARED b%(30)" (that is before CLEAR) is activeted.
As to say: if CLEAR delete and remove b%() created with "REDIM SHARED b%(30)", why "CALL ciao" works without having SHARED it after CLEAR?
Reply
#10
CLEAR and ERASE and any other similar command has always just seemed too unreliable, in my opinion.  There's just too many various rules around their behavior.  Do they affect arrays?  variables?  dynamic?  static?  ones in SUBS/FUNCTIONS?   What gets erased?  What gets freed completlely?  

FOR I = 1 to 10
   x(1) = I
NEXT

CLEAR

FOR I = 1 to 10
   PRINT x(I)
NEXT

In the above, does x(I) just get reset to 0?  Does it get erased and freed completely?  After all, there's no call to DIM it before the first FOR, so it works just fine.   IS that second FOR reusing the same variable array which has been blanked to 0, or is it a whole new array?

It's not immediately obvious what the heck is going on here, and personally I don't like that ambiguity in my code.  When I'm editing and working on something at 2 AM, after a long 18-hour day, I don't want to have to dig up some manual or wrack my brain on how some esoteric and forgotten command works.   I want to keep things simple:


FOR I = 1 to 10
   x(1) = I
NEXT

ResetArrays

FOR I = 1 to 10
   PRINT x(I)
NEXT

SUB ResetArrays
    SHARED X()
    FOR i = LBOUND(X) TO UBOUND(X)
       X(I) = 0
    NEXT
END SUB

No questions there about what's going on.   It's the same array.  Same position in memory, with the same offsets and such.  It's just had all its values reset to the default of 0.   Simple.  No questions about what's going on and what is actually doing what where.  It's resetting and initializing my arrays to 0.

What could possibly be simpler, or better than that?
Reply




Users browsing this thread: 3 Guest(s)