Re: Problem with NULL in g++

G

Gopi Subramanian

g++ should be using 0 as well. Are you sure you are using g++ and not gcc?
Are you sure the compiler knows the code is C++ and not C.

The g++ I use, has the following in the header file that defines NULL:

#undef NULL
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif

I use gcc 2.95.3 and the definition i have in stdef.h is

if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else /* G++ */
#define NULL ((void *)0)
#endif /* G++ */
#endif /* NULL not defined and <stddef.h> or need NULL. */
#undef __need_NULL
 
S

Sam Holden

I use gcc 2.95.3 and the definition i have in stdef.h is

if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else /* G++ */
#define NULL ((void *)0)
#endif /* G++ */
#endif /* NULL not defined and <stddef.h> or need NULL. */
#undef __need_NULL

Upgrade to a version which actually tries to follow the C++ standard a little...

It's free software after all :)
 
C

Corey Murtagh

Sam said:
#define NULL ((void *)0)

is *not* permitted by the standard.

After all it would make

int *pi = NULL;

illegal, which is certainly isn't.

I recall reading that one of the recent compilers (possibly one of the
GNU ones?) had an option to use a class similar to this:

--------------
struct null_t
{
template<typename T>
operator T*() const
{
return 0;
}

template<typename T>
operator ==(const T* rhs) const
{
return 0 == rhs;
}
};

template<typename T>
operator ==(const T* lhs, const null_t& rhs)
{
return 0 == lhs;
}

#undefine NULL
null_t NULL;
--------------

It's certainly interesting. Stops NULL from being used outside of
pointer operations, which of course breaks the standard, but can still
be implicitly converted to and compared against any pointer type. I
have't checked to see what the various compilers make of it from a code
generation and optimization standpoint, but it looks interesting.
 
R

Ron Natalie

Sam Holden said:
#define NULL ((void *)0)

is *not* permitted by the standard.
Yes and if you looked at the inclusion it DOES NOT define it that way.
Did you see all that ifdef stuff in there? It keeps the cast off the C++ code.
 
S

Sam Holden

Yes and if you looked at the inclusion it DOES NOT define it that way.
Did you see all that ifdef stuff in there? It keeps the cast off the C++ code.

Here's the snippet:

if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else /* G++ */
#define NULL ((void *)0)
#endif /* G++ */
#endif /* NULL not defined and <stddef.h> or need NULL. */
#undef __need_NULL

Surely G++ is the indicator for the c++ path...

And hence #define NULL ((void *)0) is defined for c++ code.

if not, then whomever thought labelling the non c++ path as G++ must
have a very strange outlook on the world.
 
R

Ron Natalie

if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else /* G++ */
#define NULL ((void *)0)
#endif /* G++ */
#endif /* NULL not defined and <stddef.h> or need NULL. */
#undef __need_NULL

Surely G++ is the indicator for the c++ path...

And hence #define NULL ((void *)0) is defined for c++ code.

if not, then whomever thought labelling the non c++ path as G++ must
have a very strange outlook on the world.

Actually, you are wrong. __GNUG__ is defined when G++ is compiling
a file. the __null is a magic token which resolves to the null pointer constant
(implemenation specific).

The comments after the #else and #endif just mark the entire conditional
block as being dependent on G++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top