[petsc-dev] #define FORTRANNULLFUNCTION(a) (((void(*)(void))(PETSC_UINTPTR_T)a) == PETSC_NULL_FUNCTION_Fortran)

Jed Brown jedbrown at mcs.anl.gov
Sat Nov 23 15:40:40 CST 2013


Barry Smith <bsmith at mcs.anl.gov> writes:

>   Jed,
>
>     It seems this PETSC_UINTPTR_T here is allowing one to cast a void
>     * to a function pointer without generating a warning/error? Is
>     this correct code? Why does it even work?

I think it is not strictly compliant because there is no guarantee that
function pointers are even the same size as normal pointers.  I don't
actually know of an architectures that does that, however, and this
particular use doesn't depend on being able to cast both ways, just that
there be no false collisions.

>     I’m trying to get rid of the last of the "casting object pointer
>     to function pointer" warning messages out of PETSc but still get a
>     truck load with
>
> #define CHKFORTRANNULLFUNCTION(a)  \
>   if (FORTRANNULLSCALAR(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLREAL(a) || FORTRANNULLINTEGER(a) || FORTRANNULLOBJECT(a)) { \
>     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
>     "Use PETSC_NULL_FUNCTION"); *ierr = 1; return; } \
>   else if (FORTRANNULLFUNCTION(a)) { a = NULL; }
>
>
> because each of the FORTRANNULLXXX() tests cast the function pointer a
> to an object pointer. Can I instead use the PETSC_UINTPTR_T trick to
> cast the object pointers like PETSC_NULL_INTEGER_Fortran to function
> pointers and compare them to eliminate all the warnings?

Or cast both sides to uintptr_t for the comparison.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20131123/3900dcae/attachment.sig>


More information about the petsc-dev mailing list