Re: Problem with NULL in g++

Discussion in 'C++' started by Gopi Subramanian, Jun 25, 2003.

  1. (Sam Holden) wrote in message news:<>...
    > On 23 Jun 2003 05:22:14 -0700, Gopi Subramanian <> wrote:
    > > I am porting a c++ source from suse linux to windows,and my code does
    > > a lot of binary file manipulation, during a debug session found that
    > > NULL values are different in VC ++ and g++, VC++ defines NULL as 0 and
    > > g++ defines it as a (void *) 0, is there any way to make sure that
    > > both of them matches ?

    >
    > 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
     
    Gopi Subramanian, Jun 25, 2003
    #1
    1. Advertising

  2. Gopi Subramanian

    Sam Holden Guest

    On 24 Jun 2003 23:39:27 -0700, Gopi Subramanian <> wrote:
    > (Sam Holden) wrote in message news:<>...
    >> On 23 Jun 2003 05:22:14 -0700, Gopi Subramanian <> wrote:
    >> > I am porting a c++ source from suse linux to windows,and my code does
    >> > a lot of binary file manipulation, during a debug session found that
    >> > NULL values are different in VC ++ and g++, VC++ defines NULL as 0 and
    >> > g++ defines it as a (void *) 0, is there any way to make sure that
    >> > both of them matches ?

    >>
    >> 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


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

    It's free software after all :)

    --
    Sam Holden
     
    Sam Holden, Jun 25, 2003
    #2
    1. Advertising

  3. Sam Holden wrote:

    > On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <> wrote:

    <snip>
    >>
    >>What's that supposed to mean? Both the versions posted are explicitly
    >>permitted by the standard (and the latter definition is pretty close to the
    >>latest version if not identical).

    >
    > #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.

    --
    Corey Murtagh
    The Electric Monk
    "Quidquid latine dictum sit, altum viditur!"
     
    Corey Murtagh, Jun 26, 2003
    #3
  4. Gopi Subramanian

    Ron Natalie Guest

    "Sam Holden" <> wrote in message news:...
    > On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <> wrote:
    > >
    > > "Sam Holden" <> wrote in message news:...
    > >
    > >>
    > >> Upgrade to a version which actually tries to follow the C++ standard a little...
    > >>
    > >> It's free software after all :)
    > >>

    > >
    > > What's that supposed to mean? Both the versions posted are explicitly
    > > permitted by the standard (and the latter definition is pretty close to the
    > > latest version if not identical).

    >
    > #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.
     
    Ron Natalie, Jun 26, 2003
    #4
  5. Gopi Subramanian

    Sam Holden Guest

    On Thu, 26 Jun 2003 08:58:02 -0400, Ron Natalie <> wrote:
    >
    > "Sam Holden" <> wrote in message news:...
    >> On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <> wrote:
    >> >
    >> > "Sam Holden" <> wrote in message news:...
    >> >
    >> >>
    >> >> Upgrade to a version which actually tries to follow the C++ standard a little...
    >> >>
    >> >> It's free software after all :)
    >> >>
    >> >
    >> > What's that supposed to mean? Both the versions posted are explicitly
    >> > permitted by the standard (and the latter definition is pretty close to the
    >> > latest version if not identical).

    >>
    >> #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.


    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.

    --
    Sam Holden
     
    Sam Holden, Jun 26, 2003
    #5
  6. Gopi Subramanian

    Ron Natalie Guest

    > 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++.
     
    Ron Natalie, Jun 26, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,096
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,234
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,371
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    26,935
    Mike Schilling
    Mar 29, 2006
  5. Bo Peng
    Replies:
    13
    Views:
    13,366
    Siemel Naran
    Jul 18, 2004
Loading...

Share This Page