Function ptr equivalent of "NULL"? 
Author Message
 Function ptr equivalent of "NULL"?

Is it possible to create something like NULL that can be used to
represent an invalid value for any function pointer (without causing the
compiler to issue a diagnostic for every assignment and comparison)?

Thanks!
--
========================================================================

========================================================================



Tue, 24 Feb 2004 04:49:45 GMT  
 Function ptr equivalent of "NULL"?

Quote:

> Is it possible to create something like NULL that can be used to
> represent an invalid value for any function pointer (without causing the
> compiler to issue a diagnostic for every assignment and comparison)?

#define NULL_FULL_PTR 0


Tue, 24 Feb 2004 05:18:01 GMT  
 Function ptr equivalent of "NULL"?

Quote:

> Is it possible to create something like NULL that can be used to
> represent an invalid value for any function pointer (without causing the
> compiler to issue a diagnostic for every assignment and comparison)?

What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
don't understand your question.

        david

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler



Tue, 24 Feb 2004 05:42:27 GMT  
 Function ptr equivalent of "NULL"?

Quote:

> What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
> don't understand your question.

Perhaps I asked a silly question.  It was my understanding that NULL is
usually defined as ((void *)0).  Since a void * is, by definition, a
pointer to a data object, assigning/comparing it to a function pointer
would be "bad".

Ben Pfaff suggested simply using something defined as 0 (with no
typecast), and this seems to work.  Why is it the obvious answer that's
hardest to see?
--
========================================================================

========================================================================



Tue, 24 Feb 2004 06:06:14 GMT  
 Function ptr equivalent of "NULL"?

Quote:

> #define NULL_FULL_PTR 0

<sound of palm hitting forehead>  Thanks!

--
========================================================================

========================================================================



Tue, 24 Feb 2004 06:13:35 GMT  
 Function ptr equivalent of "NULL"?

Quote:


> > #define NULL_FULL_PTR 0

I meant NULL_FUNC_PTR here, of course.

Quote:

> <sound of palm hitting forehead>  Thanks!

--
Moving to Palo Alto, CA on September 20, 2001, to start PhD work
at Stanford.  Anyone want to hang out after that?


Tue, 24 Feb 2004 06:16:42 GMT  
 Function ptr equivalent of "NULL"?


Quote:
>Is it possible to create something like NULL that can be used to
>represent an invalid value for any function pointer (without causing the
>compiler to issue a diagnostic for every assignment and comparison)?

0


Tue, 24 Feb 2004 06:44:39 GMT  
 Function ptr equivalent of "NULL"?

Quote:


>> Is it possible to create something like NULL that can be used to
>> represent an invalid value for any function pointer (without causing the
>> compiler to issue a diagnostic for every assignment and comparison)?
>What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
>don't understand your question.

Some machines use a different addressing system for data than they
do for executable code.  In that case, they could be very different.

-- glen



Tue, 24 Feb 2004 07:28:33 GMT  
 Function ptr equivalent of "NULL"?

Quote:


> > What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
> > don't understand your question.

> Perhaps I asked a silly question.  It was my understanding that NULL is
> usually defined as ((void *)0).  Since a void * is, by definition, a
> pointer to a data object, assigning/comparing it to a function pointer
> would be "bad".

No. (void *)0 is a "null pointer constant" which can safely be assigned
and compared to any pointer type, including pointers to functions. It
behaves just like plain 0 in this respect. The suggested "solution" is
unnecessary.

--
Joe Maun
Montreal, QC
Canada



Tue, 24 Feb 2004 07:36:19 GMT  
 Function ptr equivalent of "NULL"?


Quote:

>> Is it possible to create something like NULL that can be used to
>> represent an invalid value for any function pointer (without causing the
>> compiler to issue a diagnostic for every assignment and comparison)?

>#define NULL_FULL_PTR 0

or, if one of the headers that defines NULL is included,

        #define NULL_FUNC_PTR NULL

--
Michael M Rubenstein



Tue, 24 Feb 2004 08:41:46 GMT  
 Function ptr equivalent of "NULL"?

Quote:




> >> Is it possible to create something like NULL that can be used to
> >> represent an invalid value for any function pointer (without causing the
> >> compiler to issue a diagnostic for every assignment and comparison)?

> >#define NULL_FULL_PTR 0

> or, if one of the headers that defines NULL is included,

>    #define NULL_FUNC_PTR NULL

There is a sublety here that I had not noted before.  Normally,
data pointers may be compared with data pointers, but not with
function pointers, and function pointers may be compared with
function pointers, but not with data pointers.  Normally, I think
of ((void *) 0), which is one possible definition of NULL, as a
data pointer.  So, under these rules, I would consider comparing
a function pointer to ((void *) 0) as a mistake.  This is why I
wrote 0 instead of ((void *) 0) above.

*However*, there is a special case at work here: ((void *) 0) is
a null pointer constant, and any type of pointer, function or
data, may be compared to a null pointer constant.  So ((void *)
0), like 0, is just fine as a null function pointer or a null
data pointer.  The conclusion is that NULL also works fine as an
invalid value for any kind of pointer, function or data.

Of course, this applies only to null pointer *constants*.  You
can't assign a null pointer constant to a variable and then use
that variable's value interchangeably to compare with function
pointers and data pointers, because there is no special case that
allows that.

[The .sig quote below seems relevant.]
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman



Tue, 24 Feb 2004 10:21:28 GMT  
 Function ptr equivalent of "NULL"?

Quote:



> >> Is it possible to create something like NULL that can be used to
> >> represent an invalid value for any function pointer (without causing the
> >> compiler to issue a diagnostic for every assignment and comparison)?

> >What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
> >don't understand your question.

> Some machines use a different addressing system for data than they
> do for executable code.

Yes.

Quote:
>                          In that case, they could be very different.

No.  NULL expands to a null pointer constant, which is compatible with
any pointer type.  If the representation varies, the compiler will
take care of it.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



Tue, 24 Feb 2004 13:34:54 GMT  
 Function ptr equivalent of "NULL"?

Quote:

>> What's wrong with NULL? Any pointer type can be compared to NULL. Perhaps I
>> don't understand your question.

>Perhaps I asked a silly question.

Indeed.

Quote:
>It was my understanding that NULL is
>usually defined as ((void *)0).  Since a void * is, by definition, a
>pointer to a data object, assigning/comparing it to a function pointer
>would be "bad".

(void *)0 is special, because it is (one form of) the null pointer
constant:  

    An integral constant expression with the value 0, or such an
    expression cast to type void *, is called a null pointer constant.  If
    a null pointer constant is assigned to or compared for equality to a
    pointer, the constant is converted to a pointer of that type.  Such a
    pointer, called a null pointer, is guaranteed to compare unequal to a
    pointer to any object or function.
                          ^^^^^^^^^^^

Quote:
>Ben Pfaff suggested simply using something defined as 0 (with no
>typecast), and this seems to work.

You mean that you have a compiler that complains if you use NULL in a
function pointer context?!?

Quote:
>Why is it the obvious answer that's hardest to see?

In this case, the obvious answer is: use NULL :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Wed, 25 Feb 2004 00:13:54 GMT  
 Function ptr equivalent of "NULL"?

Quote:


>> Some machines use a different addressing system for data than they
>> do for executable code.
>Yes.

(regarding data pointers and function pointers.)

Quote:
>> In that case, they could be very different.
>No.  NULL expands to a null pointer constant, which is compatible with
>any pointer type.  If the representation varies, the compiler will
>take care of it.

OK, I once had a program, (from the GNU people) that would call
functions with 0 as an argument where a pointer was required,
and no prototype around.  This, of course, fails on a machine where
(int) and (char*) are different size, but nothing I could say would
convince them that it was wrong.

So, yes 0, NULL, or (char*)0 should all convert to a proper null
function pointer, except as an argument to a function without a prototype.
In that case, the compiler has no way to know, so it must be cast
properly.

-- glen



Wed, 25 Feb 2004 02:32:01 GMT  
 Function ptr equivalent of "NULL"?

Quote:
>OK, I once had a program, (from the GNU people) that would call
>functions with 0 as an argument where a pointer was required,
>and no prototype around.  This, of course, fails on a machine where

                                            ^^^^^
s/fails/may fail

Quote:
>(int) and (char*) are different size, but nothing I could say would
>convince them that it was wrong.

If the arguments are passed in registers, as is often the case, the broken
code may work even if they have different sizes, provided the two types
are passed using the same kind of registers.  If they are passed using
different kinds of registers, the code will fail even if the sizes are the
same.

Quote:
>So, yes 0, NULL, or (char*)0 should all convert to a proper null

                      ^^^^^
s/char*/void*

Quote:
>function pointer, except as an argument to a function without a prototype.
>In that case, the compiler has no way to know, so it must be cast
>properly.

There is another exception: variadic functions (the arguments in the
variable part).  Such functions must have a prototype in scope, but the
compiler still has no way to know, so even NULL must be explicitly cast to
the desired pointer type.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Wed, 25 Feb 2004 04:23:03 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. "Timer"Function in MC or equivalent!!

2. printf("%p\n",(void *) ptr);

3. char *ptr="Is memory allocated here?"

4. The equivalent of VB "with"

5. Equivalent of "VB Modules" in C#

6. C equivalent of "ls *.c"

7. Equivalent class of "Properties" in C#

8. Equivalent of "Inside ATL"

9. "DoEvents" equivalent

10. C++ equivalent of "DoEvents"

11. C++ equivalent of "DoEvents"

12. vc++ equivalent to "dir"

 

 
Powered by phpBB® Forum Software