Thread: Calling Function

  1. #1
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Calling Function

    I have been having some problems with an object im trying to make. I need one of the the methods to be able to figure out what function called it so it can associate it with the apropriate function in a list, without the user having to imply what function it is.

    I would like an ANSI C solution but realizing that this probably isn't possible anything is good. I have tried utilizing the stack and only can get the current functions address from it, so any help here would be great.

    Thanks in advance,
    Alex
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well there's no ANSI-C way of doing it.

    In C99, there is a predefined symbol (__func__) which is the current function name, so you could contrive something with
    foo ( __func__, my, other, parameters );
    But that's hardly "user free".

    Perhaps you should state what it is you're trying to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Actually that may help tremendously.

    Sorry for not being clearer, im making an error object and it is utilizing a Singly Linked List with the error messages in a struct with the first member being the address of the function these error messages are for. The end of any function will ret rather than return and rather than having the user of my library having to say ret(retNum,thisFunctions address) which would be INCREDIBLY reduntant, considering it would always be the same. I think that __func__ thing will do the trick though.

    Now is __func__ the name of the function or the current functions respective address?

    Edit(+):
    Just so you know im not posting off topic, I put it in C not C++ even though im working on an object, for native C code, cause I like just using the basics of C for C++, without relying on C++ specific stuff so I can convert it to C later if I need to.

    Edit(+):
    Just another thing I have wondered for a LONG time, can you imply arguments? Example:
    Code:
    void fubar(__func__,arg2,arg3,...);
    In that case I would want it to insert the address of the current function for the user as argument 1 not having them have to manually specify it. I know that C++ has this ``hidden argument'' thing for Objects(classes), the first argument or something is implied, this is EXACTLY what I need.

    Thanks alot,
    Alex
    Last edited by Axpen; 08-15-2005 at 02:45 AM. Reason: Clarity
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  4. #4
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Oh and that __func__ thing doesnt work under Turbo C or GCC, what header is it in?

    Edit(+):
    Is there any way to get a pointer to the current function, even using the stack or such?
    Last edited by Axpen; 08-15-2005 at 04:29 AM. Reason: More Info
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like I said, __func__ is new in C99.
    Which means neither C++ compiler will support it, and with dev-c++, you need to make sure you're compiling C, and probably add
    -std=c99
    to tell the compiler you're using the latest version of C.

    > Now is __func__ the name of the function or the current functions respective address?
    It's a const char *, containing the name of the function which is being compiled at the moment.

    > Just another thing I have wondered for a LONG time, can you imply arguments?
    You can probably hack something with macros, but default args are a C++ feature.

    > Is there any way to get a pointer to the current function, even using the stack or such?
    Yes, but all such things are extremely dependent on your implementation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Well the macro thing I just found out about, however I need to know how to get the address of the current function, if its using inline ASM I could just rewrite it for other OSs native inline syntax, however I just want to know if there is a way to get the current function's address without using C99 thing. I know they are pushed onto the stack, but I cant seem to get relevant addresses popping such. Any idea how one would get the current function address?
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The name of a function is its address. The following example is not guaranteed to work or be portable, but it is a Win32 example built with BC55.
    Code:
    #include <stdio.h>
    
    void foo(void)
    {
       puts("foo");
    }
    
    int main(void)
    {
       printf("main @ %p\n", (void*)main);
       printf("foo  @ %p\n", (void*)foo);
       return 0;
    }
    
    /* my output
    main @ 0040108C
    foo  @ 0040107C
    */
    I know they are pushed onto the stack, but I cant seem to get relevant addresses popping such.
    Are you really trying to use assembly calling convention for a C function? Your compiler should document such things.
    Last edited by Dave_Sinkula; 08-16-2005 at 09:33 PM. Reason: Annoying typos.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    That works if I know the function im currently in, however that's the problem. I need a way of referencing the current function im in, without knowing its name IE main or foo. I will then dynamically add that to the call with a macro, but I need a way of namelessly sending the current functions address
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Basically, you're trying to duplicate the actions of a debugger.
    Debuggers work by reading the symbol table information which is typically appended to the end of executable files, which at a minimum is the name and address of all the global functions.

    Accessing this information is of course implementation specific, but you may be able to find a library or two to make it less painful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Well what i'm wanting to do is make an error object that will have a table of errors that are accessed depending on the code returned by the current function, but it cant know what the current function is without an address of some kind (which will also be in the struct of errors and the function name in char* form too)
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM