From: david.brown@hesbynett.no   
      
   On 07/01/2026 10:25, Waldek Hebisch wrote:   
   > highcrew wrote:   
   >> On 1/6/26 3:50 PM, Waldek Hebisch wrote:   
   >>> Well, '(void)*foo' effectively says "foo" is not a null pointer   
   >>> (otherwise if would be undefined behaviour). Good optimizer   
   >>> will_not_ dereference foo, but keep information for further   
   >>> use.   
   >>   
   >> Hehe, now that I understand more of UB, that is correct, but   
   >> I don't think you can *rely* on the optimizer behavior, can you?   
   >   
   > Well, there is no warranty, but IME gcc is pretty reliable at   
   > removing such accesses.   
   >   
      
   gcc is pretty good at not evaluating an expression that is cast to void   
   (unless it has side-effects). But it is not good at using the   
   assumption that "foo" is not null in later code.   
      
      
   If you just want to tell the compiler (and human readers) that "foo" is   
   not null, you can be more explicit :   
      
      
   C23 :   
    #include    
    if (!foo) unreachable();   
      
   Newer gcc :   
       
    __attribute__((assume(foo)));   
      
   Older gcc :   
      
    if (!foo) __builtin_unreachable();   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|