Thread: What is the true address of a function ptr?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    What is the true address of a function ptr?

    I was using the following to get a memory address for functions pointers to prevent ambiguity, but unsure if this correct
    Code:
    void(*f)(void);
    
    std::cout << "Address: " << &(*f) << '\n';
    but this also works, but displays a different value, and not sure which is correct
    Code:
    void(*f)(void);
    
    std::cout << "Address: " << &(&(*f)) << '\n';
    //Just thought about it, but this one is also the same as &(f) I think
    Thank you for your assistance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    & and * always eat each other; so the first is f (i.e., the value of f, the pointer it contains), while the second is &f (the memory address where the variable f is stored).

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    well, that is something i didn't know. Thank you tabstop.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose I should be more specific, since this is C++: & and * will always eat each other, unless you've overloaded one of the operators.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, the second is invalid, and it's a quirk of GCC that it is accepted anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Actually I think the first one is invalid, because you're casting a function pointer to a void pointer, which is not allowed, because some architectures make data pointers and function pointer incompatible entities, and C is written to allow for such architectures. The second one looks fine: you're casting a function pointer pointer, which is a data pointer, to void *.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no cast in the first. However, you'll only get "1" as an output, since the implicit conversion done for output is to bool.

    The second is invalid because address-of anything is an rvalue, and you can't take the address of an rvalue.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by CornedBee View Post
    There is no cast in the first. However, you'll only get "1" as an output, since the implicit conversion done for output is to bool.
    I meant implicit cast. But if you're saying that it converts to bool that makes sense. Yet the OP seemed to imply that he was getting something that looked like a memory address. Compiler bug?

    The second is invalid because address-of anything is an rvalue, and you can't take the address of an rvalue.
    Oh, right.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yet the OP seemed to imply that he was getting something that looked like a memory address. Compiler bug?
    Good question. GCC 4.1 gives me "1". But then, GCC 4.1 also refuses the compile the second version.

    OP: What compiler do you use?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM