Thread: Finding the line and file in which a function was called

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Finding the line and file in which a function was called

    In a function, is there a way to tell where it was called from (namely, the line and file) without passing parameters? It would be my guess that __LINE__ and __FILE__ contain the line and file that the function is on, not where it was called from.

    My current solution would be to pass them as defaulted parameters:
    Code:
    void myFunc( int = __LINE__, char* = __FILE__ );
    but this seems like a bad way, since the user could specify those parameters and override the wanted behavior. Is there a better way? TIA

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Donīt think there is. A debugger should solve your problem.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Would an inline wrapper function solve the problem too? Like the wrapper, only a full-fledged C++ function:
    Code:
    void func( char *p, int line, char *file ) {
      std::cout << p << " at line " << line << " in file " << file << std::endl;
    }
    
    inline void inline_wrap( char* a ) {
      func( a, __LINE__, __FILE__ );
    }
    
    int main( ) {
      inline_wrap( "hehe" );
      return 0;
    }
    I'm hoping that since it's inline, __LINE__ and __FILE__ will refer to the point where it's being called in the file, not where I defined it. The obvious answer is I'm going to go check...

    EDIT: I just checked, no go. Still reports the line on which func() is called from inline_wrap. The macro solution will be somewhat difficult because this function is a class member template function (i.e., it looks like "some_object_name.alloc<some_type>( params )" ). Thanks for the help though.
    Last edited by roktsyntst; 04-20-2003 at 10:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM