[Oberon] How to use SYSTEM.VAL safely?
Joerg
joerg.straube at iaeth.ch
Wed Mar 31 18:07:15 CEST 2021
Jeff
For bitwise operations SYSTEM.VAL is safe.
The hint to the garbage collection is rather meant in case you manipulate POINTERs by doing calculations on them using INTEGERs.
Common low-level programming practice
BTW: If I understand correctly what you want to do with SetVal is, interprete the decimal input value as hex nbr.
SetVal(73) will store value 115 at address SEVENSEGADR.
If so, I would code it as follows:
SYSTEM.PUT(SEVENSEGADR,
val DIV 10*16 + val MOD 10);
Perhaps an other method would be to call SetVal with the hex value in the first place:
SetValue(73H);
and SerVal just does
SYSTEM.PUT(SEVENSEGADR, val)
br
Jörg
> Am 31.03.2021 um 17:17 schrieb Jeff Maggio <jmaggio14 at gmail.com>:
>
>
> Hi All!
>
> I'm new to the world of RISC5 and Oberon
>
> I'm curious about the dangers of using SYSTEM.VAL for casting or type conversion. I need to do bitwise operations on Integers and was planning on converting them to SETs using SYSTEM.VAL. However, this webpage warns me to avoid using SYSTEM.VAL because it can break garbage collection, but I wasn't able to find any of Wirth's own documentation about it. I'm assuming that means that there is a potential for a memory leak if not used properly?
>
> How can I make sure I'm using SYSTEM.VAL safely? Is the following example "safe"?
>
>
>> PROCEDURE SetVal*(val : INTEGER);
>> (* only values between 0 and 99 are valid*)
>> VAR digit1, digit2 : SET;
>> BEGIN
>> (* Convert the integer into 2 binary-coded decimal digits*)
>> digit1 := SYSTEM.VAL(SET, val DIV 10);
>> digit2 := SYSTEM.VAL(SET, val MOD 10);
>> digit1 := LSL(digit1, 4);
>> SYSTEM.PUT(SEVENSEGADR, digit1 + digit2);
>> END SetVal;
>
>
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20210331/3e8a8cc0/attachment.html>
More information about the Oberon
mailing list