Redefining NULL was RE: Re: Managing accidental read/writes to addresszero
"Martin.J Thompson" <[email protected]>
| Newsgroups | gmane.comp.hardware.motorola.microcontrollers |
|---|---|
| Message-ID | <[email protected]> |
> > >>>> [email protected] 17 May 2005 20:21:19 >>> > >> You can't redefine NULL, > >Actually that's not true. There is nothing in either the ANSI or ISO >standards that forbids redefining the preprocessor macro NULL. It's not on >the list of pre-defined macros that cannot be redefined, and in fact is a >"standard language addition", meaning you do not *have* to use the >definition that comes with the compiler at all. > >> it is *defined* by the language to be "0" in a >> pointer context, > >Also not true, the macro NULL is not guaranteed to have a particular value >in *any* context. > Indeed, quite true - sorry, got my cases confused in the process of trying to emphasise things! Doh! What I was trying to say (confusingly!) was that it has always been my understanding that null (lower-case) is defined to be zero in the source file and then the compiler deals with it howver it has been written to do so. >> the compiler then turns it into whatever it's internal >> representation of a null pointer is (which may or may not be a valid >> memory location, and may not be location 0) > >Every C implementation I've ever used has either #define NULL 0 or #define >NULL ((void *)0). There is unfortunately a lot of legacy code in the world >that assumes (NULL == 0) is a true expression. It's not good code, it's >non-portable and tends to be confusing, but it exists and any compiler that >breaks it will likely be considered to have a bug (in spite of what the >standards say). > Maybe so. In which case redefining NULL is not a good plan :-) >> See http://www.eskimo.com/~scs/C-faq/s5.html, esp Qs 5.2, 5.4, and for >> people like me (hardware enigneers who hack C occasionally) 5.15! > >The author of that page is clearly confused ... apparently he does not >remember that C is case-sensitive. The sections he quotes as saying that >NULL is the same as 0 uniformly say that a pointer assigned to be 0 is null, >which is not the same thing at all ! Except for the one that says "... null >pointer constants, *usually* referred to by the name NULL ...". Note the >implication that there can be multiple null pointer constants ... > Indeed there can, for different types for example. >However, the whole "redefining NULL" discussion misses the point. The main >issue is that pointers *which have never been initialized* are frequently 0. >Thus, dereferencing an uninitialized pointer (rather than one that has been >deliberately initialized to NULL) is the real problem the OP is trying to >address. > And that's different problem - which redefining NULL is not going to help with, as pointers which have not been explicitly initialised may be "referencing address 0" or may be "reference address garbage" depending on how they were allocated. Ahh pointers - what fun we can have with them :-) Cheers, Martin -- Martin Thompson CEng MIEE TRW Conekt, Stratford Road, Solihull, B90 4GW. UK +44 (0)121-627-3569 : [email protected] http://www.conekt.net/ ----------------------------------------------------------- To learn more about Freescale Microcontrollers, please visit http://www.freescale.com/mcu Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/MPC500/ <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/