home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.forth      Forth programmers eat a lot of Bratwurst      117,927 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 116,296 of 117,927   
   Anton Ertl to Paul Rubin   
   Re: push for memory safe languages -- im   
   11 Mar 24 21:08:43   
   
   From: anton@mips.complang.tuwien.ac.at   
      
   Paul Rubin  writes:   
   >anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:   
   >>>2+2=5 is also deterministic yet wrong.   
   >> In Java 2+2 gives 4.  What do you hope to gain by putting up straw men?   
   >   
   >2+2=5 is obviously wrong and Java doesn't go quite that far.  Java   
   >instead insists that you can add two positive integers and get a   
   >negative one.  That's wrong the same way that 2+2=5 is.   
      
   Not at all.  Modular arithmetic is not arithmetic in Z, but it's a   
   commutative ring and has the nice properties of this algebraic   
   structure.   
      
   >It just doesn't   
   >mess up actual programs as often, because the numbers involved are   
   >bigger.   
      
   If you use members of that ring as if they were members of Z, you will   
   sometimes get an unintended result; but even that works surprisingly   
   well, so well that the RISC-V designers have not seen a need to   
   include an efficient way to detect those cases where the result   
   deviates from that in Z.  Still, the nice algebraic properties of   
   modular arithmetic can be of benefit even in such cases:   
      
   9223372036854775807 1 + dup cr . 2 - cr .   
      
   prints   
      
   -9223372036854775808   
   9223372036854775806  ok   
      
   in Gforth on a 64-bit machine.   
      
   >In what world can it be right for n to be a positive integer and n+1 to   
   >be a negative integer?  That's not how integers work.   
      
   It's how Java's int and long types work.  And if you want something   
   closer to Z, Java also has BigInteger.   
      
   >Tony Hoare in 2009 said about null pointers:   
      
   And the relevance is?   
      
   >Java-style wraparound   
   >arithmetic is more of the same.  A bug magnet,   
      
   Unsupported claim.  Interestingly, I remember only one case where I   
   saw an unintended result due to modular arithmetic in a programming   
   language.  It happened when I computed with performance counter   
   results in bash.  bash still works that way:   
      
   [~:147654] A=9223372036854775807   
   [~:147655] echo $[A+1]   
   -9223372036854775808   
      
   I think I saw the unintended result on a 32-bit machine, because   
   performance counter results typically do not exceed 2^48, definitely   
   not 2^63-1.   
      
   >Java also has null pointers, another possible mistake.  Ada doesn't have   
   >them,   
      
   Ada certainly has null.   
      
   >C++ has them because of its C heritage and   
   >the need to support legacy code, but I believe that in "modern" C++   
   >style you're supposed to use references instead of pointers, so you   
   >can't have a null or uninitialized one.   
      
   I don't know much about C++, but I would be surprised if they had   
   given up on uninitialized data.  And an uninitialized reference is   
   certainly not better than a null reference.   
      
   Null pointers are at least a little bit more on-topic in this thread   
   than integer overflow.  In Java one can write, say, a linked list or a   
   tree in an object-oriented manner, with, e.g., a tree node being an   
   abstract class that has two concrete subclasses: inner node, and empty   
   node.  No null pointers in sight, right?  Wrong: When an inner node is   
   created, the constructor of the node first sees a data structure where   
   all bytes have been initialized to 0, in order to guarantee memory   
   safety; for the references to the child nodes, this means that at that   
   point they are null pointers.  Only then can the Java code in the   
   constructor overwrite them with whatever proper value they get.  Is it   
   a problem?  Not if they only exist there.   
      
   The fact that Java idiomatics is to implement trees and linked lists   
   not in the object-oriented way I outlined above, but in an imperative   
   way with null pointers instead of empty nodes could be more   
   problematic, but is it a major problem?  Not in my (limited)   
   experience.   
      
   - anton   
   --   
   M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html   
   comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html   
        New standard: https://forth-standard.org/   
      EuroForth 2023: https://euro.theforth.net/2023   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca