0

I recently attended a coding interview and I was asked a question which I didn't know the answer to. After searching the answer on the internet for a few day, I come here call for help.

The question is described as followed: You should propose a approach to record the running information of function in your program, for example, the times of a function called, and so on.

By the way, you are not allowed to modify these functions. Maybe you want to define a global variant in these function to record the running function, but that is not allowed.

Ok! That's all about the question I met in a coding interview.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
L_Yang
  • 39
  • 3
  • If you want this question to receive more attention and this is a language specific question, you should add the applicable language tag (just one please). If this is not a language-specific question, consider using the [tag:language-agnostic] tag. – Bernhard Barker Sep 13 '13 at 15:07

1 Answers1

0

This is the best I could come up with using C++ macros. I don't know whether it conforms to the requirements.

A very basic version just recording the count. The macro replaces all existing calls to the function with the contents of the macro, which records the stats and calls the function. Can easily be extended to record more details. Assumes there's only one function with that name or you want one count for all of them. Requires a macro for each function.

// here's our function
void func()
{ /* some stuff */ }

// this was added
int funcCount = 0;
#define func(...) do { funcCount++; func(__VA_ARGS__); } while(0)

int main()
{
    // call the function
    func();

    // print stats
    cout << funcCount << endl;

    return 0;
}

Prints 1.

A more generic version. Requires changes to how the function is called.

// here are our functions
void someFunc()
{ /* some stuff */ }
void someOtherFunc()
{ /* some stuff */ }

// this was added
map<string, int> funcCounts;
#define call(func, ...) do { funcCounts[ #func ]++; func(##__VA_ARGS__); } while(0)

int main()
{
    // call the functions
    // needed to change these from 'someFunc();' format
    call(someFunc);
    call(someOtherFunc);
    call(someFunc);

    // print stats
    for (map<string, int>::iterator i = funcCounts.begin(); i != funcCounts.end(); i++)
      cout << i->first << " - " << i->second << endl;

    return 0;
}

Prints:

someFunc - 2
someOtherFunc - 1
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Thanks! But your approach are similar to the method that uses a global variant. I mean that how we can record the information of function called while we didn't modify the program. In some development tool,for example visual studio 2012, have a debug tool which can analysis the times and frequency of the functions called. So i want to how this debug tool implement this function. @Dukeling – L_Yang Sep 13 '13 at 09:08
  • Any way to record stats regarding a function call would have to be somewhat similar to this, although it could look a little different or be on a lower level (i.e. assembler / machine code). You have code which you want to have executed before and after the function is called, so logically you'll have to, when the function is called, go to the code you want executed before, then call the function, then go to the code you want executed after. – Bernhard Barker Sep 13 '13 at 09:21
  • Even [breakpoints do something similar](http://stackoverflow.com/questions/3915511/how-do-breakpoints-work-in-c-code). For x86, this appears to be done by passing control to the debugger at that point. This follows the same theme of redirecting the execution of the program to where the stats are recorded. Recording function stats can just be implemented as a breakpoint at the start and one at the end of the function (although I'm not sure it's done like this). – Bernhard Barker Sep 13 '13 at 09:28