Forum
CLEAR - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Wishlist (https://www.boriel.com/forum/forumdisplay.php?fid=14)
+---- Thread: CLEAR (/showthread.php?tid=2339)



CLEAR - zarsoft - 03-04-2023

How do I clean an ARRAY?

I would like to use the function CLEAR.

Code:
VAR A(3) TYPE INTEGER

PROCEDURE CLEAR (v)
FOR i = ADDRESS(v) TO ADDRESS(v)+SIZE(v)-1
  POKE i,0
NEXT i
END PROCEDURE

PROCEDURE Clean
CLEAR A
END PROCEDURE

PROCEDURE Test
LET A(1) = 1
PRINT A(1)
Clean
PRINT A(1)
END PROCEDURE



RE: CLEAR - boriel - 03-06-2023

There is a faster way, use memset:

Code:
#include <memcopy.bas>

PROCEDURE CLEAR (v)
    MEMCOPY(ADDRESS(v), 0, SIZE(v))
END PROCEDURE

Please, try this and tell me if it works.

Also, I'm curious: How did you implement ADDRESS(v) and SIZE(v), they can be useful and included in the compiler.
(The next version will have changes and will update the standard library).


RE: CLEAR - zarsoft - 03-06-2023

I have not implemented these functions.
I was hoping you had already implemented them.


RE: CLEAR - boriel - 03-06-2023

No. To get the address of an element, you normally use the @ operator.
I.e. @a returns the memory address (uInteger) of the [a] variable. For arrays, @arr won't work as expected as it will return the address of the array's metadata. To get the address of the 1st element you will use @arr(0, 0).

I will come back with a solution on the size, but it might be not that strightforward.


RE: CLEAR - zarsoft - 03-06-2023

CLEAR an array would be very useful.