[HN Gopher] The difference between undefined behavior and ill-fo...
       ___________________________________________________________________
        
       The difference between undefined behavior and ill-formed C++
       programs
        
       Author : st_goliath
       Score  : 9 points
       Date   : 2024-08-03 21:08 UTC (1 hours ago)
        
 (HTM) web link (devblogs.microsoft.com)
 (TXT) w3m dump (devblogs.microsoft.com)
        
       | rileymat2 wrote:
       | > Avoiding code paths with undefined behavior is something you do
       | all the time.
       | 
       | > // Check the pointer before using it
       | 
       | > if (p != nullptr) p->DoSomething();
       | 
       | I love this example.
        
       | Bjartr wrote:
       | > However, if your program avoids the code paths which trigger
       | undefined behavior, then you are safe.
       | 
       | This seems incorrect as demonstrated by the other undefined
       | behavior story I read on HN today[1], the tl;dr of which, as I
       | understood it, is since UB is not allowed, the compiler can elide
       | checks that would protect against UB for the sake of optimization
       | since a correct program wouldn't have caused the UB in the first
       | place and the compiler doesn't have to respect the semantics of
       | incorrect programs.
       | 
       | [1] https://news.ycombinator.com/item?id=41146860
        
         | nlewycky wrote:
         | The compiler may remove the nullptr check in:
         | ptr->foo = 1;       if (ptr == nullptr)          return;
         | 
         | but it may not remove the nullptr check in:                 if
         | (ptr == nullptr)          return;       ptr->foo = 1;
        
       ___________________________________________________________________
       (page generated 2024-08-03 23:00 UTC)