Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Problem with NULL in g++

Reply
Thread Tools

Re: Problem with NULL in g++

 
 
Gopi Subramanian
Guest
Posts: n/a
 
      06-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Sam Holden) wrote in message news:<(E-Mail Removed) >...
> On 23 Jun 2003 05:22:14 -0700, Gopi Subramanian <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      06-25-2003
On 24 Jun 2003 23:39:27 -0700, Gopi Subramanian <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Sam Holden) wrote in message news:<(E-Mail Removed) >...
>> On 23 Jun 2003 05:22:14 -0700, Gopi Subramanian <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Corey Murtagh
Guest
Posts: n/a
 
      06-26-2003
Sam Holden wrote:

> On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <(E-Mail Removed)> 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!"

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      06-26-2003

"Sam Holden" <(E-Mail Removed)> wrote in message news:(E-Mail Removed). ..
> On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <(E-Mail Removed)> wrote:
> >
> > "Sam Holden" <(E-Mail Removed)> wrote in message news:(E-Mail Removed). ..
> >
> >>
> >> 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.


 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      06-26-2003
On Thu, 26 Jun 2003 08:58:02 -0400, Ron Natalie <(E-Mail Removed)> wrote:
>
> "Sam Holden" <(E-Mail Removed)> wrote in message news:(E-Mail Removed). ..
>> On Wed, 25 Jun 2003 10:30:29 -0400, Ron Natalie <(E-Mail Removed)> wrote:
>> >
>> > "Sam Holden" <(E-Mail Removed)> wrote in message news:(E-Mail Removed). ..
>> >
>> >>
>> >> 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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      06-26-2003
> 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++.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments