473,898 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine calling function


Hello,

I have implemented a small library with a function a datatype to
manage temporary storage, and handle out correctly casted storage. The
function to get a double pointer is for instance:

double * work_get_double (work_type *work, size_t size) {}
Now, if the work area is not sufficiently large, the function fails,
with a call to abort. In the case of failure, I would *very much*
like to know the name of the function calling work_get_double (), so if
function foo() calls work_get_double (), and the request can not be
satisfied, I would like work_get_double () to fail with something like:

fprintf(stderr, "Sorry call from function: %s could not satsified. Aborting\n",CAL LER_NAME)
abort();

Where CALLER_NAME, in this case would resolve to 'foo'. Is something
like this possible (without actually passing in the function name manually)?
Best Regards

Joakim Hove
--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Nov 15 '05 #1
21 3607
You need to explicitly pass the caller name to the function as a
seperate arg !
To get a fn. name at runtime you can use the macro __FUNC__
(implementation dependant)

- Ravi
Joakim Hove wrote:
Hello,

I have implemented a small library with a function a datatype to
manage temporary storage, and handle out correctly casted storage. The
function to get a double pointer is for instance:

double * work_get_double (work_type *work, size_t size) {}
Now, if the work area is not sufficiently large, the function fails,
with a call to abort. In the case of failure, I would *very much*
like to know the name of the function calling work_get_double (), so if
function foo() calls work_get_double (), and the request can not be
satisfied, I would like work_get_double () to fail with something like:

fprintf(stderr, "Sorry call from function: %s could not satsified. Aborting\n",CAL LER_NAME)
abort();

Where CALLER_NAME, in this case would resolve to 'foo'. Is something
like this possible (without actually passing in the function name manually)?
Best Regards

Joakim Hove


Nov 15 '05 #2

Hello,
You need to explicitly pass the caller name to the function as a
seperate arg !
OK - I was afraid of that.
To get a fn. name at runtime you can use the macro __FUNC__
(implementation dependant)


OK - that is it at least better than fully mannually typing in the
function names. On linux/gcc-3.2.3 the variable __FUNCTION__ is
defined.

Thanks for your help.
Joakim :-)
--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Nov 15 '05 #3
Joakim Hove a écrit :
Hello,

You need to explicitly pass the caller name to the function as a
seperate arg !

OK - I was afraid of that.

To get a fn. name at runtime you can use the macro __FUNC__
(implementati on dependant)

OK - that is it at least better than fully mannually typing in the
function names. On linux/gcc-3.2.3 the variable __FUNCTION__ is
defined.


In C99, __func__ is standard.
Nov 15 '05 #4
ajm
of course you can "overload" work_get_double by defining a macro that
inserts the __func__ argument on your behalf and encourage users to use
the macro e.g.,

work_get_double _impl(work_type *work, size_t size, const char *caller)
{
....your implementation as it currently is...
}

and then define

#define work_get_double (a,b) work_get_double _impl((a),(b),_ _func__)

so that when users call work_get_double (a,b) the caller name is passed
"automatica lly"

the usual function macro caveats apply but the above is not uncommon,

hth,
ajm.

Nov 15 '05 #5
Le 14-10-2005, Joakim Hove <ho**@ntnu.no > a écrit*:
I have implemented a small library with a function a datatype to
manage temporary storage, and handle out correctly casted storage. The
function to get a double pointer is for instance:

double * work_get_double (work_type *work, size_t size) {}

Now, if the work area is not sufficiently large, the function fails,
with a call to abort. In the case of failure, I would *very much*
like to know the name of the function calling work_get_double (), so if
function foo() calls work_get_double (), and the request can not be
satisfied, I would like work_get_double () to fail with something like:

fprintf(stderr, "Sorry call from function: %s could not satsified. Aborting\n",CAL LER_NAME)
abort();

Where CALLER_NAME, in this case would resolve to 'foo'. Is something
like this possible (without actually passing in the function name manually)?


A common solution is to use macro:
#define WORK_GET_DOUBLE (work, size) work_get_double (work,size,__fu n__)

Marc Boyer
Nov 15 '05 #6

Hello,
A common solution is to use macro:
#define WORK_GET_DOUBLE (work, size) work_get_double (work,size,__fu n__)


thanks to both ajm and Marc for the macro based solution; I see that I
can/will work but I am somewhat (maybe undeservedly so?) weary about
macros.

Joakim

--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Nov 15 '05 #7
Le 15-10-2005, Joakim Hove <ho**@ntnu.no > a écrit*:
A common solution is to use macro:
#define WORK_GET_DOUBLE (work, size) work_get_double (work,size,__fu n__)


thanks to both ajm and Marc for the macro based solution; I see that I
can/will work but I am somewhat (maybe undeservedly so?) weary about
macros.


You can. Macro have a lot of (well ?) known drawbacks, but
sometimes, it is the best (or less worst) compromise.

Marc Boyer
Nov 15 '05 #8

Joakim Hove wrote:
Now, if the work area is not sufficiently large, the function fails,
with a call to abort. In the case of failure, I would *very much*
like to know the name of the function calling work_get_double (), ...


Since you plan to *abort* anyway then, assuming abort() doesn't do
any special cleanup or printing, the minimal keystroke approach
which *I* often use is to core-dump.
The function names on the call stack are usually the first thing
printed by a debugger, e.g. in response to "where" when running
gdb. How to coredump? On most systems the simplest approach
would be to *ignore* memory allocation failure and just attempt
to use the (presumably null) pointer!

Of course one would never do things this way in *delivered* code,
but good delivered code probably shouldn't suffer from allocation
failures, at least the kind that lead to abort. I'm guessing
that you're *developing* code that isn't *yet* at the perfection
level you seek.

I'll bet 15-to-1 this message draws very hot flames, but frankly
I find the mantra "Always check malloc()'s return code" to be
very much over-dogmatic in many contexts. (For one thing, an OS
like Linux allows *so much* memory to be malloc()'ed that it will
thrash painfully long before malloc() actually "fails".)

The dogma seems particularly silly in cases like yours where
referencing the invalid pointer achieves *precisely* what you
want: a core dump, with visible call stack, etc.

Detractors will point out that a similar effect can be achieved
without violating the dogma. I reply: Yes, but spend the extra
coding minutes checking for a different error that *might
actually occur*, or where special diagnostic prints might be useful.

Donning my asbestos suit...
James D. Allen

Nov 15 '05 #9
Joakim Hove <ho**@ntnu.no > writes:
Hello,

I have implemented a small library with a function a datatype to
manage temporary storage, and handle out correctly casted storage. The
function to get a double pointer is for instance:

double * work_get_double (work_type *work, size_t size) {}
Now, if the work area is not sufficiently large, the function fails,
with a call to abort. In the case of failure, I would *very much*
like to know the name of the function calling work_get_double (), so if
function foo() calls work_get_double (), and the request can not be
satisfied, I would like work_get_double () to fail with something like:


OT: Run a debugger like gdb when the program is aborted, and inspect the
call stack.

/Niklas Norrthon
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2766
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__ is a special case ). The closest to this I could find was the 2002-12-04 posting 'metaclasses and static methods' by Michele Simionato. The posting's example metaclass uses the method's name. I present my own example of automatic method kind...
17
6173
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
3
1711
by: mikeorb | last post by:
Is there a way to determine the object that a property is in? For example: function MyClass() { ... } MyClass.prototype.myFunc = function() { ... } var obj = new MyClass(); var sameobj = SOMEFUNCTION(obj.myFunc); So my goal is to get a reference to the object by just having the
2
5206
by: Edward | last post by:
I have the following function to determine if a form is loaded: Public Function modIsloaded(ByVal vstrFormName As String) As Boolean ' Error handling removed for concision ' Determine if form open modIsloaded = False If (SysCmd(acSysCmdGetObjectState, acForm, vstrFormName) = acObjStateOpen) Then
21
1672
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list functions, I thought I could do something like: if L.index('A') < L.index('D'): # do some stuff But I didn't know if maybe there was a preferred method for this type of
25
6563
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library as a small tool function according to its quality? I will be very happy to get your suggestion from every aspect on it: interface design, C language knowledge or algorithm efficient. Sincerely,
6
14394
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future, excluding holidays and weekends.' I didn't want to build a table of holidays that would have to be continuously updated, so I searched high and low for a function that would tell me whether a given date was a holiday, to no avail. I did find an...
10
43650
by: John Brown | last post by:
Hi there, Does anyone know how to (generically) determine the currently active form for an application using a "static" function (so I can call it from anywhere). There is no offiical way I've been able to find so I've written the following for starters: public static Form GetActiveForm() { // Returns null for an MDI app Form activeForm = Form.ActiveForm; if (activeForm == null) {
5
1941
by: MLH | last post by:
Access 97 does not provide a means of reading the most recent setting for SetWarnings Method. For example, if you had CBF that called a procedure in a global module and the following statement was executed: DoCmd.SetWarnings False Then you would not be able to take peek at the setting when processing returned to the calling
0
9994
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10859
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10956
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7191
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6079
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.