Mailinglist Archive: opensuse-programming (118 mails)

< Previous Next >
Re: [suse-programming-e] Programming standards!
  • From: Jerry Feldman <gaf@xxxxxxx>
  • Date: Wed, 20 Apr 2005 12:58:03 -0400
  • Message-id: <200504201258.03082.gaf@xxxxxxx>
On Wednesday 20 April 2005 11:38 am, Michael Stevens wrote:
> On Wednesday 20 April 2005 14:16, Jerry Feldman wrote:
> > On Tuesday 19 April 2005 7:39 pm, Philipp Thomas wrote:
> > > That's from the pre-ANSI days, when void and 'void *' didn't exist.
> > > The C standard defines NULL to be (void *)0.
> >
> > Not entirely true:
> > "NULL which expands to an implementation-defined null pointer
> > constant". ISO/IEC 9899:1999
>
> A rather vague definition. I was recently looking at what to do with NULL
> in C++. So I looked it up in Stroustroup. In section 5.1.1 "Zero" there
> is the following paragraph:
>
> In C, it has been popular to define a macro NULL to represent the zero
> pointer. Because of C++ tighter type checking, the use of plain 0, rather
> then and suggested NULL macro leads to fewer problems. If you feel you
> must use NULL, use "const int NULL = 0;" The const qualifer prevents
> accidental redefinition of NULL and ensures that NULL can be used where a
> constant is required.
>
> I really don't understand what Bjarne means. Particularly the mention of
> 'tighter type checking' and 'fewever problems' seem odd. Of course macros
> are evil so that is a good reason not to use NULL. The ISO C++ standard
> only refers to NULL in the conext of the <cxxx> header files.
The C++ language has true constants.
First, every C++ function must be fully prototyped unlike C.
NULL is a macro defined in stdio.h. In the C++ context, it might be more
proper to use:
const void * NULL = 0;
Then, the C++ NULL constant is a true pointer with a value of 0.
const int NULL = 0 is somewhat problematical in a 64-bit environment, since
pointers are 64-bits. But, since C++ is fully prototyped, it will be
widened appropriately.
--
Jerry Feldman <gaf@xxxxxxx>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9

< Previous Next >
Follow Ups