[Bug c++/35669] New: NULL (__null) not considered different from 0 with C++
l dot lunak at suse dot cz
gcc-bugzilla@gcc.gnu.org
Sat Mar 22 20:06:00 GMT 2008
When compiling with g++, the various error checking gained from using NULL
instead of plain 0 for the null pointer does not work. For example
$ cat a.c
#include <stdio.h>
void foo( int a )
{
printf( "INT\n" );
}
int main()
{
foo( NULL );
foo( 0 );
int a = NULL;
return 0;
}
$ gcc -Wall a.c
a.c: In function ÂmainÂ:
a.c:10: warning: passing argument 1 of Âfoo makes integer from pointer without
a cast
a.c:12: warning: initialization makes integer from pointer without a cast
a.c:12: warning: unused variable ÂaÂ
$ g++ -Wall a.c
a.c: In function Âint main()Â:
a.c:12: warning: unused variable ÂaÂ
As can be seen, compiling as C++ does not detect the mistakes in the code. The
compiler uses __null though, as can be seen in preprocessed code:
$ g++ -Wall -E a.c | tail
{
printf( "INT\n" );
}
int main()
{
foo( __null );
foo( 0 );
int a = __null;
}
$ gcc -v
Using built-in specs.
Target: i586-suse-linux
Configured with: ../configure --prefix=/usr --with-local-prefix=/usr/local
--infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib
--libexecdir=/usr/lib --enable-languages=c,c++,objc,fortran,obj-c++,java,ada
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3
--enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/
--with-pkgversion='SUSE Linux' --disable-libgcj --with-slibdir=/lib
--with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --program-suffix=-4.3
--enable-version-specific-runtime-libs --enable-linux-futex
--without-system-libunwind --with-cpu=generic --build=i586-suse-linux
Thread model: posix
gcc version 4.3.0 (SUSE Linux)
The problem already exists with gcc 4.2.1, but gcc 4.1.2 (specifically "gcc
version 4.1.2 20070115 (prerelease) (SUSE Linux)") works fine and reports:
$ g++ -Wall a.c
a.c: In function Âint main()Â:
a.c:10: warning: passing NULL to non-pointer argument 1 of Âvoid foo(int)Â
a.c:12: warning: converting to non-pointer type Âint from NULL
a.c:12: warning: unused variable ÂaÂ
--
Summary: NULL (__null) not considered different from 0 with C++
Product: gcc
Version: 4.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: l dot lunak at suse dot cz
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35669
More information about the Gcc-bugs
mailing list