creating a new instance of a function?

This is a discussion on creating a new instance of a function? within the C++ Programming forums, part of the General Programming Boards category; ok, this kinda follows on my previous post. (i got basic user function calls executing, as well as user created ...

  1. #1
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    creating a new instance of a function?

    ok, this kinda follows on my previous post. (i got basic user function calls executing, as well as user created functions being processed and executed) however i need to know how to create a new instance of a function. example.
    Code:
    void bob(std::string unused_dont_ask_why)
    {
    	std::cout << "blah";
    }
    could i do something like this:
    Code:
    void (*)(string) the_func_pointer = new(bob);
    how would i create a function pointer to bob such that it would have a unique address?

    thanks guys,

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't by any language means.

    However you could try to find the code of the function and allocate a block of memory with execute rights and copy the code there and call this unique function.

    That is, if the function doesn't use absolute or relative addresses in a way that will give you difficulties.




    But... why do you want this 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

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    First of all functions don't have instances. Instances are related to processes and threads.

    AFAIK a function pointer looks like one of the following. I've done this before but I totally forget what I did. Since I'm not at my compiler right now I cannot test this.

    <return type> *FunctionName(<param list>);

    or

    <return type> (*FunctionName)(<param list>);


    Again this looks kinda jiggy so I'm not positive this is correct. But you can do function pointers in C/C++. I used to use XMS which required me to attain a function pointer to the XMShandler which was the main handler for the XMS routines. The address of the handler was return to me via an XMS call and then I assigned my function to point to that handler's address effectively creating a C-like function.

    This is similar to the format for an interrupt handler in C.

    interrupt (*MyHandler)(void) - for C
    interrupt (*MyHandler)(...) - for C++

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This syntax is the correct one:
    <return type> (*pointerName)(<param list> );

    Or what I prefer because it's much easier to use:
    typedef <return type> (*FUNC_PTR_TYPE_NAME)(<param list>);
    FUNC_PTR_TYPE_NAME pointerName;
    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

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You can create a function pointer on the heap;

    Code:
    #include <iostream>
    
    void foobar()
    {
    	std::cout << "Hello World";
    }
    
    int main()
    {
    	typedef void(*FP)();
    	
    	FP *ptrf = new FP(&foobar);//excuse lack of error handling
    	
    	(*ptrf)();
    	
    	delete ptrf;
    }
    But I cant see the need......functions pointers are very small (4 bytes on modern systems) and they are finite........so why bother?

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Thanks a million CornedBee. Just wasn't sure which one was right. Love the typedef - much easier.


  7. #7
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    ok, i tried fordys answer and it basically works how i wanted. however it leaves me with another problem.
    for my code to work i will need the function thats being called to know its address.

    Code:
    #include <iostream>
    
    void foobar()
    {
    	std::cout << ADDRESS_OF_THIS_COPY_OF_FUBAR << std::endl;
    }
    
    int main()
    {
    	typedef void(*FP)();
    	FP *ptrf = new FP(&foobar);//excuse lack of error handling
    	(*ptrf)();
    	(*foobar)();
    	delete ptrf;
    	return 1;
    }
    thanks

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    To get the address of a func you could

    std::cout << std::hex << "Address of this func = " << &foobar;

    But I cant understand what you are doing......

    If you think you can create a whole new instance of a block of code, then CornedBee has already explained that you cant

    What are you trying to achieve?

  9. #9
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    i dont need a new block of code, i need the same block of code accessable from 2 seperate addresses that i could call using pointers. i need a unique address for each function, even if they are identical in what they do.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Why do you need unique addresses?

  11. #11
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    its required for an index.

  12. #12
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Do you think you could explain why you're doing this with a little more detail than "for an index"? I (and others quite clearly) would be very interested in knowing what in the world you are doing this for. Thanks.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    There is a very dangerous element to creating functions on the heap. Let's say some moron programmer deletes said function using delete and then calls it later. Very bad medicine my friends.
    But Fordy is right in that you could approach it that way but I would definitely limit access to that function via a class so that it could not be deleted accidentally or otherwise. And Fordy was not recommending that you do it that way if I know Fordy as well as I think I do. He is merely showing another way to do it, which is what he specializes in.

    i dont need a new block of code, i need the same block of code accessable from 2 seperate addresses that i could call using pointers. i need a unique address for each function, even if they are identical in what they do.
    If they are the same function (or block of code) why the heck do you need two unique addresses for each function???

    Perhaps and illustration in assembly would suffice.

    Code:
    __LI   proc
    ARGS v1:DWORD,v2:DWORD,f1:QWORD
    
    push bp
    mov  bp,sp
    
    fild [v2]
    fisub [v1]
    fmul [f1]
    fiadd [v1]
    
    pop bp
    
    ret
    
    __LI  endp
    
    extern "C" double LI(unsigned long v1,unsigned long v2,double f1)
    Now LI will be linked in (assuming you have included the source file or the binary file in the link) and the compiler will give it an address.

    If you need to access this function from your program, you simply call it like this:

    LI(v1,v2,f1);

    or in assembly

    call __LI

    or

    call <some address>

    Thankfully we can use function names so we don't have ugly looking call instructions sitting around. Since there are no classes and no protection mechanisms in this code - LI can be called from anywhere inside of your program. So if it is the same block of code - it does not need 2 distinct addresses because that would simply be a copy of the original function and would waste space.
    In C this would be flagged as an error and would not compile since the function prototypes would be exactly the same. The compiler will not create two copies of the same function and place them at distinct addresses and you should not either.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What Fordy did was irrelevant actually. He didn't allocate a function on the heap, he allocated a function pointer. Use: zero.
    What he ended up with was a pointer to a pointer to a function.

    There is a very dangerous element to creating functions on the heap. Let's say some moron programmer deletes said function using delete and then calls it later. Very bad medicine my friends.
    Actually I think this is just as likely as said scenario:
    [code]void (*myfunc)();
    myfunc = ((void) (*)())0x123AFE91;
    myfunc();
    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

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by CornedBee
    What Fordy did was irrelevant actually. He didn't allocate a function on the heap, he allocated a function pointer. Use: zero.
    What he ended up with was a pointer to a pointer to a function.
    What I gave him looked pretty close to what he was trying to do in the original thread. I didnt say I was allocating code on the heap.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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, 09:18 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21