home bbs files messages ]

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

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

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

   Message 242,757 of 243,242   
   Andrey Tarasevich to Andrey Tarasevich   
   Re: NULL dereference in embedded [was: O   
   03 Jan 26 18:44:02   
   
   From: noone@noone.net   
      
   On Sat 1/3/2026 5:24 PM, Andrey Tarasevich wrote:   
   > On Sat 1/3/2026 3:25 PM, highcrew wrote:   
   >> On 1/4/26 12:15 AM, highcrew wrote:   
   >>> I have a horrible question now, but that's for a   
   >>> separate question...   
   >>   
   >> And the question is:   
   >>   
   >> Embedded systems.  Address 0x00000000 is mapped to the flash.   
   >> I want to assign a pointer to 0x00000000 and dereference it to   
   >> read the first word.   
   >> That's UB.   
   >>   
   >> How do I?   
   >   
   > Well, the first question would be: what is the physical null pointer   
   > representation in that C implementation on that embedded system?   
   > ...   
      
   Although, on the second thought, what I said above, while correct, is   
   hardly relevant to the matter if using UB for optimizations.   
      
   UB-based optimizations rely on static analysis of the code during   
   compilation. At that stage the platform-specific physical representation   
   of null pointer plays no role at all. The only thing that matters is the   
   ability of the compiler to identify and track _logical_ null pointers   
   through the program. E.g. for the compiler   
      
      int *p = 0;   
      
   is always a null pointer. And   
      
      if (p != 0)   
      
   always checks pointer `p` for being null. The actual physical   
   representation of `p` does not come into the picture at all.   
      
   In that case the key moment here is that only compile-time zero (i.e.   
   integral constant expression zero) can be interpreted as a null pointer.   
   A run-time zero cannot be.   
      
   And the only issue that remains is your original request "I want to   
   assign a pointer to 0x00000000 and dereference it to read the first   
   word". Well, firstly, the language does not offer you any   
   standard-defined features for accessing specific addresses. But in   
   real-life it is usually done through explicitly converting an integer   
   address to a pointer type. Since   
      
      int *p = 0;   
      
   has a reserved meaning and will not generally work as intended, one   
   possible workaround would be   
      
      uintptr_t a = 0;   
      int *p = (int *) a;   
      
   In the above case `p` will not be seen by the compiler as a logical null   
   pointer.   
      
   This is actually covered by the FAQ: https://c-faq.com/null/accessloc0.html   
      
   --   
   Best regards,   
   Andrey   
      
   --- 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