Thread: Address of a Function

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Address of a Function

    If i wanted to find the address of a variable i would do this:

    int x = &y;

    How would I do the same thing for a function that i wrote? In this program:
    Code:
    void hello(); 
    
    void hello()
    {
         cout << "hello" << endl;
    }
    
    int main(){
        cout << &hello;
        cin.get();
        return 0;
    }
    cout << &hello always evaluates to 1. How would i get the memory address?

    THANKS!
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I see no reason to try and print the address of a function, and the MinGW port of g++ warns me that the address of a function will always evaluate to true, which would explain your output of 1. Are you trying to use function pointers? If so, I would expect a toy example like this:
    Code:
    typedef void (*Function)();
    Function func = hello;
    func();
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I recall correctly, to print the value of a pointer you want to cast it to void*. (How meaningful that is, who knows.)

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    So functions don't have locations in memory where they start like variables do? Ah, it's probably to complex of an problem for me. Ok, so we'll scrap that. I do have another question though, kindof related.

    How would i write to a specific memory location in the program? For example, say i want to change 4 bytes at offset location 0x12345678 in my program, how would i do that? Is there a function like, writetomemory() or something?

    Now, along those same lines, say a place in memory held a asm instruction, and i wanted to change that asm instruction. How would i go to that memeory location, and change the bytes by writing asm code?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by jrahhali View Post
    So functions don't have locations in memory where they start like variables do? Ah, it's probably to complex of an problem for me. Ok, so we'll scrap that. I do have another question though, kindof related.

    How would i write to a specific memory location in the program? For example, say i want to change 4 bytes at offset location 0x12345678 in my program, how would i do that? Is there a function like, writetomemory() or something?

    Now, along those same lines, say a place in memory held a asm instruction, and i wanted to change that asm instruction. How would i go to that memeory location, and change the bytes by writing asm code?
    Just set the value of a pointer using a cast and dereference it, and voila.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by EVOEx View Post
    Just set the value of a pointer using a cast and dereference it, and voila.
    ...you get a crash, most likely.
    Point being, you should not do that because the OS will prevent you from doing so.
    This type of thing should only be done in low-level embedded systems and such which requires you to read / write from certain locations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    ...you get a crash, most likely.
    Point being, you should not do that because the OS will prevent you from doing so.
    This type of thing should only be done in low-level embedded systems and such which requires you to read / write from certain locations.
    Ah, but I've done it several times. When I was working on overflows. Which, in my opinion, every good programmer should have done several times to be able to understand the problem and prevent them properly.

    And when testing around with stuff like that, I did use this to find out addresses of functions (although it's better to use a debugger, but I didn't know that back then), and to test changing stuff with a memory address directly.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not good. It's forbidden knowledge
    But seriously, it's a bad idea because it can corrupt and crash your program. Such techniques are used in low-end hardware, not PCs.
    And you can take the address of functions. Plus what you are doing is much low level. Stuff you can expect with low-end hardware, certainly not PCs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    It's not good. It's forbidden knowledge
    But seriously, it's a bad idea because it can corrupt and crash your program. Such techniques are used in low-end hardware, not PCs.
    And you can take the address of functions. Plus what you are doing is much low level. Stuff you can expect with low-end hardware, certainly not PCs.
    Well, of course you should not actually release a program that contains any of these features. However, like I said, for investigating on how your compiler works and how to exploit vulnerabilities, this may be very useful.

    Just for fun:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    void hello()
    {
            cout << "Hello, world" << endl;
    }
    
    int main()
    {
            unsigned int test;
            unsigned int *ptr = &test;
    
            ptr += 2;
            for(int i = 0; i < 10; i++)
                    *ptr++ = (unsigned int)hello;
    }

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When taking the address of a function, there is no guarantee that the address you obtain is the actual address of the first instruction of the function. It is only required that whatever address it is, calling through a function pointer pointing to that address will invoke the function in question.

    Taking function addresses is only ever done in order to create function pointers, at least in portable code. That's not to say there's no other reason to do it, just that you're off in platform-specific land.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by EVOEx View Post
    Well, of course you should not actually release a program that contains any of these features. However, like I said, for investigating on how your compiler works and how to exploit vulnerabilities, this may be very useful.
    I fail to see how it is useful.
    I would throw the code out the window since it's so undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I fail to see how it is useful.
    To understand how attacks might be constructed, and thus to become more proficient in identifying vulnerabilities and to understand better how to avoid them.

    Now, I am going to put on my moderator hat: jrahhali, your questions appear to be headed in a direction that is not allowed by our forum guidelines. ("5. Messages relating to cracking, (erroneously called "hacking" by many), copyright violations, or other illegal activities will be deleted. Due to the overlapping boundaries of code with malicious intent, and other legitimate uses of it, the moderators will assess each potential infraction on a case by case basis.")

    As such, I am closing this thread.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM