This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: free() and implicit conversion to a function pointer


Going by subj and talk below, this is a bit confusing...

But it looks like you are testing 'free' for a value?

Isn't standard 'free' declared to take 1 arg and
return void?

If you aren't talking standard 'free()', then
nevermind...


Hans-Bernhard Bröker wrote:
[Sorry, forgot to reply-all...]

Am 15.03.2017 um 23:48 schrieb Jeffrey Walton:

Since Coverity is
complaining about an implicit conversion, maybe the following will
help to avoid the implicit part (and sidestep the finding):

    if (free != NULL)
        break;

Or perhaps:

    if ((void*)free != NULL)
        break;

Even setting aside that the latter should of course have been

     if ((void*)free == NULL)
         break;

those are both worse than the original code. (void *) is _not_ suitable for use with function pointers. Neither is NULL in the general case, because it may very well be ((void *)0).

The reason this is wrong is that C by design treats data and functions as living in separate realms, i.e. its virtual machine has a Harvard architecture. One of the consequences of this is that pointers to functions and pointers to data are incommensurable, i.e. any and all conversions or comparisons across this divide are wrong. (void *) are compatible to all data pointers, but not to function pointers.

The only code that might actually be a slight bit better than the given

    if (! free)

would be

    if (0 != free)

The function designator `free' auto-decays into a function pointer, which is compared to a null pointer constant: 0. The ! operator does that same thing implicitly, but is fully equivalent to it.
---
Free autodecays to a function pointer?
In what language?

It's not a C-function nor a C function pointer.



--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]