correct definition of NULL

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

correct definition of NULL

Jonathan Gray
As pointed out by Richard Guenther our definition
of NULL will be incorrect for c++ compilers that aren't
gcc (or pretend to be gcc like clang).

Index: dirent.h
===================================================================
RCS file: /cvs/src/include/dirent.h,v
retrieving revision 1.21
diff -u -p -r1.21 dirent.h
--- dirent.h 6 Apr 2011 11:39:42 -0000 1.21
+++ dirent.h 2 Jul 2011 07:18:49 -0000
@@ -84,6 +84,8 @@ typedef struct _dirdesc {
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif /* __GNUG__ */
Index: locale.h
===================================================================
RCS file: /cvs/src/include/locale.h,v
retrieving revision 1.7
diff -u -p -r1.7 locale.h
--- locale.h 6 Apr 2011 11:39:42 -0000 1.7
+++ locale.h 2 Jul 2011 07:18:49 -0000
@@ -59,6 +59,8 @@ struct lconv {
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: stddef.h
===================================================================
RCS file: /cvs/src/include/stddef.h,v
retrieving revision 1.11
diff -u -p -r1.11 stddef.h
--- stddef.h 6 Apr 2011 11:39:42 -0000 1.11
+++ stddef.h 2 Jul 2011 07:18:49 -0000
@@ -67,6 +67,8 @@ typedef __mbstate_t mbstate_t;
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: stdio.h
===================================================================
RCS file: /cvs/src/include/stdio.h,v
retrieving revision 1.39
diff -u -p -r1.39 stdio.h
--- stdio.h 6 Apr 2011 11:39:42 -0000 1.39
+++ stdio.h 2 Jul 2011 07:18:49 -0000
@@ -58,6 +58,8 @@ typedef __off_t off_t;
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: stdlib.h
===================================================================
RCS file: /cvs/src/include/stdlib.h,v
retrieving revision 1.48
diff -u -p -r1.48 stdlib.h
--- stdlib.h 6 Apr 2011 11:39:42 -0000 1.48
+++ stdlib.h 2 Jul 2011 07:18:49 -0000
@@ -80,6 +80,8 @@ typedef struct {
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: string.h
===================================================================
RCS file: /cvs/src/include/string.h,v
retrieving revision 1.21
diff -u -p -r1.21 string.h
--- string.h 6 Apr 2011 11:39:42 -0000 1.21
+++ string.h 2 Jul 2011 07:18:49 -0000
@@ -46,6 +46,8 @@ typedef __size_t size_t;
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: time.h
===================================================================
RCS file: /cvs/src/include/time.h,v
retrieving revision 1.19
diff -u -p -r1.19 time.h
--- time.h 6 Apr 2011 11:39:42 -0000 1.19
+++ time.h 2 Jul 2011 07:18:49 -0000
@@ -47,6 +47,8 @@
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: unistd.h
===================================================================
RCS file: /cvs/src/include/unistd.h,v
retrieving revision 1.63
diff -u -p -r1.63 unistd.h
--- unistd.h 6 Apr 2011 11:39:42 -0000 1.63
+++ unistd.h 2 Jul 2011 07:18:49 -0000
@@ -58,6 +58,8 @@
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif
Index: wchar.h
===================================================================
RCS file: /cvs/src/include/wchar.h,v
retrieving revision 1.17
diff -u -p -r1.17 wchar.h
--- wchar.h 1 Jun 2011 16:39:07 -0000 1.17
+++ wchar.h 2 Jul 2011 07:18:49 -0000
@@ -65,6 +65,8 @@
 #ifndef NULL
 #ifdef __GNUG__
 #define NULL __null
+#elif defined(__cplusplus)
+#define NULL 0L
 #else
 #define NULL ((void *)0)
 #endif

Reply | Threaded
Open this post in threaded view
|

Re: correct definition of NULL

Karl Sjodahl - dunceor-2
On Sat, Jul 2, 2011 at 11:16 AM, Jonathan Gray <[hidden email]> wrote:

> As pointed out by Richard Guenther our definition
> of NULL will be incorrect for c++ compilers that aren't
> gcc (or pretend to be gcc like clang).
>
> Index: dirent.h
> ===================================================================
> RCS file: /cvs/src/include/dirent.h,v
> retrieving revision 1.21
> diff -u -p -r1.21 dirent.h
> --- dirent.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.21
> +++ dirent.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -84,6 +84,8 @@ typedef struct _dirdesc {
> B #ifndef NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif /* __GNUG__ */
> Index: locale.h
> ===================================================================
> RCS file: /cvs/src/include/locale.h,v
> retrieving revision 1.7
> diff -u -p -r1.7 locale.h
> --- locale.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.7
> +++ locale.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -59,6 +59,8 @@ struct lconv {
> B #ifndef NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define NULL B  __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: stddef.h
> ===================================================================
> RCS file: /cvs/src/include/stddef.h,v
> retrieving revision 1.11
> diff -u -p -r1.11 stddef.h
> --- stddef.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.11
> +++ stddef.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -67,6 +67,8 @@ typedef B  B  B  __mbstate_t B  B  mbstate_t;
> B #ifndef B  B  B  B NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define NULL B  __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: stdio.h
> ===================================================================
> RCS file: /cvs/src/include/stdio.h,v
> retrieving revision 1.39
> diff -u -p -r1.39 stdio.h
> --- stdio.h B  B  6 Apr 2011 11:39:42 -0000 B  B  B  1.39
> +++ stdio.h B  B  2 Jul 2011 07:18:49 -0000
> @@ -58,6 +58,8 @@ typedef B  B  B  __off_t off_t;
> B #ifndef NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: stdlib.h
> ===================================================================
> RCS file: /cvs/src/include/stdlib.h,v
> retrieving revision 1.48
> diff -u -p -r1.48 stdlib.h
> --- stdlib.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.48
> +++ stdlib.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -80,6 +80,8 @@ typedef struct {
> B #ifndef B  B  B  B NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define NULL B  __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: string.h
> ===================================================================
> RCS file: /cvs/src/include/string.h,v
> retrieving revision 1.21
> diff -u -p -r1.21 string.h
> --- string.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.21
> +++ string.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -46,6 +46,8 @@ typedef B  B  B  __size_t B  B  B  B size_t;
> B #ifndef B  B  B  B NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: time.h
> ===================================================================
> RCS file: /cvs/src/include/time.h,v
> retrieving revision 1.19
> diff -u -p -r1.19 time.h
> --- time.h B  B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.19
> +++ time.h B  B  B 2 Jul 2011 07:18:49 -0000
> @@ -47,6 +47,8 @@
> B #ifndef B  B  B  B NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: unistd.h
> ===================================================================
> RCS file: /cvs/src/include/unistd.h,v
> retrieving revision 1.63
> diff -u -p -r1.63 unistd.h
> --- unistd.h B  B 6 Apr 2011 11:39:42 -0000 B  B  B  1.63
> +++ unistd.h B  B 2 Jul 2011 07:18:49 -0000
> @@ -58,6 +58,8 @@
> B #ifndef NULL
> B #ifdef B  B  B  B  __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
> Index: wchar.h
> ===================================================================
> RCS file: /cvs/src/include/wchar.h,v
> retrieving revision 1.17
> diff -u -p -r1.17 wchar.h
> --- wchar.h B  B  1 Jun 2011 16:39:07 -0000 B  B  B  1.17
> +++ wchar.h B  B  2 Jul 2011 07:18:49 -0000
> @@ -65,6 +65,8 @@
> B #ifndef B  B  B  B NULL
> B #ifdef __GNUG__
> B #define B  B  B  B NULL B  B __null
> +#elif defined(__cplusplus)
> +#define B  B  B  B NULL B  B 0L
> B #else
> B #define B  B  B  B NULL B  B ((void *)0)
> B #endif
>
>

Is there a reason for that NULL is defined at so many places?

BR
Dunceor

Reply | Threaded
Open this post in threaded view
|

Re: correct definition of NULL

Philip Guenther-2
On Sat, Jul 2, 2011 at 3:33 AM, Dunceor <[hidden email]> wrote:
...
> Is there a reason for that NULL is defined at so many places?

Because the standards say so.  If the code to do so grows complicated
enough (it's close already, IMHO) then perhaps that definition should
be moved to an internal header (say, <_null.h>) and then all those
files would include that instead.


Philip Guenther