2

I got this compiler warning:

Ordered comparison of function pointers ('IMP' (aka 'id(*)(id,SEL,...)')and "IMP")

When i try to compare two entity:

for (i = 0; k < 30; k++) {
        IMP imp = getValue(class_method[i]);
        if (imp <= (IMP)currDesc->address) {//this is the line causing the warning
          size_t diff = (size_t)classMap->address - (size_t)imp;
          if (diff < mediumSize) {
            best_method = class_method[i];
            is_class_method = YES;
            mediumSize = diff;
          }
        }

Can you please explain me the eventual cause of this warning and how to fix it.

zoul
  • 102,279
  • 44
  • 260
  • 354
Luca
  • 20,399
  • 18
  • 49
  • 70

1 Answers1

4

In direct answer to the question, the ordered comparison of function pointer warning is generated when you try to compare the addresses of two function pointers. In most cases, this comparison is erroneous because there is little value in comparing the location of two functions in memory.

That may or may not be the case here, but if you need to have the function pointers be directly comparable, you will need to either explicitly turn off the warning (I couldn't find the compiler option for this, if there is one) or cast the two pointers to values that support comparison, such as void*.

logancautrell
  • 8,762
  • 3
  • 39
  • 50
gaige
  • 17,263
  • 6
  • 57
  • 68
  • Except that in C you can not cast a functionpointer to void * portably, as a functionpointer could be larger than void *. IMO the original compiler warning is un-C like anyway. – user3093235 Apr 23 '19 at 09:19
  • @user3093235 See https://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer for the spec's comments on `void *`; the pointer type itself will be the same regardless of whether it's a function pointer, a `void *`, or a `char *`. The specification explicitly states that function pointers may be converted to `void *` and back. – gaige Apr 23 '19 at 13:37
  • I don't see that in the answer linked. Quite the opposite actually: `So no; no guarantee that a void * can hold a function pointer.` Please link to the actual C spec, where it says so (it doesn't). – user3093235 Sep 06 '19 at 10:04
  • Setting aside that we're talking about Objective-C and iOS, not specifically generic C and clearly not a portability question originally, you are correct that the portion of the C99 standard quoted in the answer I pointed to does not indicate that `void *` and a function pointer will be the same size. `void*` referenced in my original answer was used as an example of a last-ditch mechanism after I'd already suggested against the comparison, and works in the context of the original question. I still maintain that any comparison other than equality of function or method pointers is a bad idea. – gaige Sep 06 '19 at 18:23