[petsc-dev] Problem with PETSC_NULL and variadic functions

Satish Balay balay at mcs.anl.gov
Wed Nov 9 15:15:26 CST 2011


How about using:

<petscsys.h>
extern void* PETSC_NULL
<init.c>
static void* PETSC_NULL=0L [or null or nullptr - based on configure determined flags]

[so as to avoid language/functionality based #def() code in petscsys.h
- that could potentially cause issues like what we had with isinf()
stuff]

Satish

On Wed, 9 Nov 2011, Jed Brown wrote:

> On Wed, Nov 9, 2011 at 14:51, Barry Smith <bsmith at mcs.anl.gov> wrote:
> 
> >  In C we could do #define PETSC_NULL          ((void*) 0) and every thing
> > would be fine
> >
> 
> yes
> 
> 
> >
> >    In C++ this won't work and there is no alternative except a standard
> > that is 2 months old (but for those programming in C++ this is not really a
> > problem because there is really no need for varargs in proper C++ code).
> >
> 
> unless they want to call our variadic functions
> 
> 
> >
> >  Questions:
> >     1) Why won't it work in C++? Note that PETSC_NULL truly is suppose to
> > always be used as a null pointer and should never be used as 0; if int 0 is
> > intended then int 0 should be used.
> >
> 
> C++ needs an explicit cast to convert from void*. So
> 
> some_type *x = (void*)0;
> 
> is a compilation error. It's too much to ask for an explicit cast any time
> PETSC_NULL is passed.
> 
> 
> >
> >     2) Can we at least fix it for C by using #define PETSC_NULL
> >  ((void*) 0)  in C and using 0 in C++. After all nobody really uses PETSc
> > from C++ :-)
> >
> 
> This will work. They should be able to pass nullptr in C++11, NULL with GCC
> and some other compilers, or (void*)0 only in variadic slots (because those
> types aren't checked by the compiler).
> 




More information about the petsc-dev mailing list