473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

name of the calling function

15
When executing a C++ program, How can I find out the name of the function that is currently being executed. If assume my program main is calling a function f1() and that in turn is calling f2(). Then at any given time how can I find out which function has completed execution.
i am creating an automatic garbage collector and I need to know where the program control is during collection phase so that the memory of the functions that have finished execution is collected.
Apr 10 '09 #1
5 2049
JosAH
11,448 Expert 8TB
When a function has finished execution its stack frame is lost and possibly reused by other functions, i.e. you are too late then; you have to invoke your garbage collector just before that stack frame is lost. The layout of those frames is highly implementation dependent. Check Hans Boems garbage collector and see through what hoops it has to go in order to find pointers to reclaimable memory.

kind regards,

Jos
Apr 10 '09 #2
Theoritically :
What you are talking about is the functionality of code a profiler. Basically a compiler should have the ability to instrument your functions so that some code is executed at the entry and exit of your function.

In practise :
The above paragraph talks about what is available in dynamic languages. From experience In the systems languages world such support is obtuse and hard to find (intel compilers do it I think, MS compilers can dump out timing information).

Method I use(and have seen in complex software):
I stick function prolog and epilogue macros in my code. These macros execute code which (a) captures timing information (b) inserts a blanket try catch block for every function in order to locate exception sources in code.

Now you could take a similar approach. At the start of every function write the function name to a std C++ stack object (as a global of course) and pop it at the end. Also write the stack out at each function entry.

Conclusion:
There are a hundred different ways of doing this and serious software will include instrumentation like this.
Apr 10 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Conclusion:
There are a hundred different ways of doing this and serious software will include instrumentation like this.
This is a simple trace log usually implemented using a circular file holding enough entries for a crash autopsy. This is done all the time in production code.

Trace entries also track values of the incoming and outgoing variables plus any other pertinant information (like the calling function name). Just stick the name where the tracer can find it.

Trace functions can have the ability to read a parameter file so that incoming values can be altered arbitrarily to assist in a debug.

The trace functions are equipped with a monitor that checks if the trace is on. If not, the trace function does nothing. This avoids slowdowns when crashes are not being autopsied.
Apr 10 '09 #4
vsachar
15
Actually what i also want to know is how can a function know which function called it?
void main()
{
add();
}
in the above example how will add know, who called it?
Apr 11 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
You would need to set this up in your own code.

Maybe:
Expand|Select|Wrap|Line Numbers
  1. enum Function {main, add, none,etc...)
  2.  
  3. int main()
  4. {
  5.     Function = main;
  6.     add();
  7. }
  8.  
  9. void add()
  10. {
  11.    if (Function == main)
  12.    {
  13.       // it was main 
  14.   }
  15. }
I don't know why you would want to do this unless you are debugging and if that's the case you should just use your debugger to look at the call stack.
Apr 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Woof-Woof | last post by:
Why doesn't my mouse wheel work in the code windows in VB5 under XP? I can scroll the properties and project windows using the wheel, but not the actual code windows (XP). Everything works fine...
21
by: TheKeith | last post by:
I heard that the name attribute is deprecated in html 4.01 strict. Is it recommended that you use the ID attribute for images along with the getElementById method instead of the old way? Thanks.
6
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
2
by: Don | last post by:
This may seem like an odd question, but is it possible to get the name of the class & function that is calling a function of another class? e.g. Public Class CallerName Public Shared Function...
8
by: Jeffrey Barish | last post by:
I believe that the answer to my question is no, but I want to be sure that I understand this issue correctly: Suppose that there are two classes defined as follows: class A(object): def...
3
by: Rico | last post by:
Hello, I have a generic process that logs errors from different sources. When I call this code, I'd like to also submit the name of the function or sub that is raising the error without having...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
12
by: mcarthybn | last post by:
hi , I am working on huge C programming project.I am doin debugging only by printf measn i dont have any dubugger .So it is very difficult know who is actually calling a function and i am stuck...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.