Function origin

This is a discussion on Function origin within the C++ Programming forums, part of the General Programming Boards category; When I call a function X in multiple places (main, from within other functions) is it possible to know each ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Function origin

    When I call a function X in multiple places (main, from within other functions) is it possible to know each time X was called what part of the code called it, or do I have to pass flags with each call telling the function X where it was called ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,202
    I assume you want to do this in code, rather than using development/debugging tools (which rely on support by the compiler to include information they can use).

    The only way is to provide the information yourself (eg additional arguments). For example;
    Code:
    #include <iostream>
    
    void func(const char *file, int line)
    {
        std::cout << "Called from source file " << file << " Line " << line << '\n';
    }
    
    int main()
    {
         func(__FILE__, __LINE__);
    }
    In C (as per 1999 standard) also supports a __func__ predefined identifier that gives the name of the calling function. This is not supported in C++ or the preceding 1989/90 C standard. There is no direct way to get the line number relative to a function; __LINE__ represents the line number in the source file.

    In C++, if you want to pass the calling function name, you have to do it yourself. For example;
    Code:
    #include <iostream>
    
    void func(const char *caller)
    {
        std::cout << "Called by " << caller <<  '\n';
    }
    
    int main()
    {
        const char function_name[] = "int main()";
         func(function_name);
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    There are non-standard extensions, of course. They might be preferable to hand-coding like that. You might even define the macros depending on if it's compiler X or the rest to make it portable.
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But the real question is, why do you want to know?
    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
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Because I have a function that prints contents of an array, it is used multiple times.
    The first line is:
    Code:
    cout << "Array  " << what << " = ";
    what can be "created", "updated", "deleated" ...
    Right now I am passing it along with the array to the function.

    So I wanted to know is there a different way to do this.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by Suchy View Post
    what can be "created", "updated", "deleated" ...
    Right now I am passing it along with the array to the function.
    Sounds like the right way of doing it. Why do you seek an alternative? I don't see how the name of the array necessarily relates to the name of the function which calls the array print function... In fact, coupling the two borders on insane.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use something like the StackTrace class I created in this thread: StackTrace implementation?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Thanks for the replys, I will stick with my original way.
    But thanks for the ideas.

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, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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