Thread: Calling function from its memory address?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Calling function from its memory address?

    I searched the internet for a method of doing this, and came across this neat little trick:

    Code:
    typedef void func(void);
    func* f = (func*)0xdeadbeef;
    f();
    And I'm a little stuck here on understanding the concept of this. Is the 0xdeadbeef suppose to act as a null/garbage value?

    Also, can

    Code:
    func* f = (func*)0xdeadbeef;
    just be written as:

    Code:
    func *f = (func*)0xdeadbeef;
    Also, what's the point of (func*)?

    Sorry for the rather newbie question, however I can't quite understand what's happening here. Thanks for the help!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    well normally you would do something like:
    Code:
    void actual_func(void);
    typedef void ( *func)(void);
    
    func fp = &actual_func;
    
    // Call func.
    fp();
    I don't know why you would want to refer to a specific address instead of a specific function name instead. I mean, especially with modern systems, how are you to know the real address? I suppose that is part of the rationale for why C++ doesn't let you assign addresses like numbers, you have to cast them to a pointer type. Function pointers themselves are still useful on some occasions; this may or may not be one of them.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Ah, thank you. I guess I just got a little confused on what I was wanting to do there.

    Your example is actually what I wanted to do. Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    0xdeadbeef is just a placeholder address for example purposes, similar to "foo", "bar", etc. The address has no significant meaning in C++.

    The location of the pointer operator can indeed be in either of the configurations you gave, or even in the form "func * f" with a space on both sides.

    The point of the "(func*)" is to typecast the address to be of the func* type (effectively void because of the preceeding typedef, which is a special type of pointer). See Pointers - C++ Documentation for more on function pointers and void pointers.

    Edit: I do believe whiteflag's suggestion is a more proper usage, but unless I'm mistaken, both should "work".
    Last edited by blakeo_x; 12-29-2012 at 11:27 PM. Reason: Ninja'd by whiteflag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Problem with memory clean up in calling function
    By karthur3 in forum C Programming
    Replies: 0
    Last Post: 03-22-2009, 01:50 PM
  3. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  4. I have the Memory Address of a function, how do I call it?
    By vulcan_146 in forum C++ Programming
    Replies: 8
    Last Post: 05-22-2005, 02:00 AM
  5. Memory address?
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:04 PM