Thread: Function pointer comparison

  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    Function pointer comparison

    On the comparison of function pointers http://www.newty.de/fpt/fpt.html#compare:
    Code:
    if(pt2Function >0)
    Are function pointers when compared with integer values guaranteed to be unsigned? Otheerwise, this would fail for instance in kernel code or where the code located above 2GB in a 32-bit system, right?

    I'm personally not sure if pointers are signed or unsigned... Anyone know?

    I would do if (pt2Function != 0) instead - as zero is probably the only value that is sane to use, besides the address of a function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe the behaviour is undefined since pointer comparison with the relational operators is not defined for a comparison between a pointer to an object and a null pointer (constant). I would also just compare for equality (or inequality) with a null pointer constant.

    Furthermore, the preceding text ("You can use the comparison-operators (==, !=) the same way as usual.") hints that the mistake is a typographical error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    FWIW, I know of one 64-bit architecture system that doesn't have this problem - IBM z/Architecture (mainframes).

    The reason it doesn't have this problem is because executable code can only reside below the 2GB address range. You can put data all the way up to 16 exabytes, but code is still below the 2GB "bar".

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by matsp View Post
    Are function pointers when compared with integer values guaranteed to be unsigned? Otheerwise, this would fail for instance in kernel code or where the code located above 2GB in a 32-bit system, right?
    You did mean 64-bit system, right? Because, I don't know how you would get code above 2GB on a 32-bit system.
    Last edited by Dino; 08-28-2008 at 08:07 AM.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    And yet another thought...

    On a 64-bit system, the pointers should be... drum roll please... 64 bits, not 32 bits, so the concept of signed vs unsigned shouldn't come into play.

    (Or, am I just rambling because I didn't read the link posted that kicked off this topic?)
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    And yet another thought...

    On a 64-bit system, the pointers should be... drum roll please... 64 bits, not 32 bits, so the concept of signed vs unsigned shouldn't come into play.

    (Or, am I just rambling because I didn't read the link posted that kicked off this topic?)
    Well, it does - but only when you reach the boundary between 7FFF FFFF FFFF FFFF and 8000 0000 0000 0000 - which is quite unlikely in the current hardware architectures [unless we have severely scattered memory layout - which of course is possible or even likely - but perhaps not that much scattered].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    64 bits, not 32 bits, so the concept of signed vs unsigned shouldn't come into play.
    What the number of bits in the type does to do with the signed/unsigned?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by vart View Post
    What the number of bits in the type does to do with the signed/unsigned?
    An address of X'F0000000' on a 32 bit system could be evaluated as a signed integer, and it would be negative.

    That same address on a 64-bit system would be X'00000000 F0000000', and if evaluated as a (long) integer, it would not be negative.

    Right?
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by matsp View Post
    Well, it does - but only when you reach the boundary between 7FFF FFFF FFFF FFFF and 8000 0000 0000 0000 - which is quite unlikely in the current hardware architectures [unless we have severely scattered memory layout - which of course is possible or even likely - but perhaps not that much scattered].

    --
    Mats
    You mean 7FFF FFFF FFFF FFFF and FFFF FFFF FFFF FFFF, right? Otherwise, you're talking about a single byte worth of addressing.
    Last edited by Dino; 08-28-2008 at 08:30 AM. Reason: typo - had bit, should be byte
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Right?
    Yes this particular address will be positive, while as Mats has shown, there are other adresses, that still could be interpreted as negative...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    You mean 7FFF FFFF FFFF FFFF and FFFF FFFF FFFF FFFF, right? Otherwise, you're talking about a single byte worth of addressing.
    When you go above 7 (15*F) a signed 64-bit integer turns negative, and does so for the 2^63 numbers up to FFFF FFFF FFFF FFFF, just like in 16-bits ANYTHING above 7FFF is negative [and much easier to write!] all the way to FFFF.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Correct, I understand. And I now understand that you meant FFFF FFFF FFFF FFFF and not 8000 0000 0000 0000.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dino View Post
    Correct, I understand. And I now understand that you meant FFFF FFFF FFFF FFFF and not 8000 0000 0000 0000.
    I'm guessing there's not quite understanding yet. We want two addresses that are right next to each other, so that we can have a boundary. 7FFF FFFF FFFF FFFF and 8000 0000 0000 0000 are addresses that are right next to each other, so there's a boundary between them. The first is (or can be) interpreted as positive, the second is (or can be) interpreted as negative, so it's a boundary that means something. 7FFF FFFF FFFF FFFF and FFFF FFFF FFFF FFFF are a very long way apart, and there can not be a boundary between them, since they are not adjacent.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Correct, I understand. And I now understand that you meant FFFF FFFF FFFF FFFF and not 8000 0000 0000 0000.
    Actually, I meant 8 and lots of zeros - because I meant that you cross the boundary of 7(fill with F) and 8(Fill with 0) - The former is positive, the latter negative - and everything above [above in the unsigned world] that is obviously also negative, all the way to all F's.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thanks guys. I really do understand all this. The terminology I (was) stuck on was "when you reach the boundary between...".

    You can't "reach" the boundary between, because there is nothing (no address) between 7F's and 80's, so you can only cross over the boundary.

    However after letting this sink in some more, this phrase is fine. I have typically used other phrases to denote the change in addressing limits.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM