473,320 Members | 1,933 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,320 software developers and data experts.

Way to retrieve calling function's ID to print an error?

Hi - I am trying to replace a macro:

myassert(e) ((e) ? (void)0 : _assert(__FILE__, __LINE__, "e"))

which is used in ~800 places in my code and it's taking up too much space. I want to replace it with a single function call which tests for 0, but I still need to print the line number and file name where the error came from. Is there a way in C to get that info?? thank you
Nov 21 '11 #1
5 1598
donbock
2,426 Expert 2GB
In what way does this macro take up too much space?

_assert is not a standard macro. Please explain what it accomplishes for you. (It is better for you to tell me than for me to guess.)

Is there a reason why you're using a private myassert macro instead of the standard assert macro? See assert.h.

By the way, does that last argument to _assert expand properly? It looks like you're trying to stringify the argument to myassert.
Nov 22 '11 #2
You're right donblock : I should have mentioned what the modified assert does. We have to use _assert because we are printing stuff over a serial port using our own API, and it prints out a message and line number and from which module the error came from. And, the "e" should have been #e - it copied from the wrong ifdef...

I am working on fw and am running out of space, and from what I understand, macros are always expanded inline so it seemed (verified) to be cheaper to call a function to test for 0 rather than have that macro. My teammates said we have to keep the macro in order to print the line number and source, and I am trying to find a way around that so we can have ~22 KB back. thanks
Nov 22 '11 #3
donbock
2,426 Expert 2GB
The most obvious way to save space without changing the functionality is to instantiate a single string with the name of the source file rather than a separate copy within each myassert expansion. See if your compiler has a switch that collapses multiple instances of an identical string into a single instance of that string.

If your compiler does not have that capability, then add the following line near the top of each source file and replace __FILE__ within myassert with myname.
Expand|Select|Wrap|Line Numbers
  1. static const char myname[] = __FILE__;
It would be more convenient to add the definition of myname to the header file that defines the myassert macro, but that presents two possible problems.
  1. Defining variables within a header file can cause all sorts of problems unless you're very careful.
  2. Sometimes (perhaps all the times) __FILE__ within a header file expands to the name of the header file rather than the name of the source file that included the header file.
Nov 22 '11 #4
gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)


i couldn't find a flag to collapse multiple instances of a macro but that would be my ideal solution.

if i define the myname array in each (~150) header, i have to add a parameter to my assert macro or is there something magical happening in the background?

Expand|Select|Wrap|Line Numbers
  1. #define assert(e, f) ....
  2.  
Nov 29 '11 #5
donbock
2,426 Expert 2GB
You don't want to collapse multiple instances of a macro -- you want to collapse multiple instances of the same string literal.

Consider a source file that invokes myassert 10 times. The expansion of each invocation passes __FILE__ to _assert. That is, the value of __FILE__ (a string literal containing the filename) occurs 10 times. Your program would be smaller if all myassert invocations referred to a single string literal.
Nov 29 '11 #6

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

Similar topics

3
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
2
by: flupke | last post by:
Hi, i have a property in a class that gets changed and i would want to know who changes it. Is there a way i can find out the calling function of a property? Thanks, Benedict
6
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference...
4
by: astri | last post by:
#include "Unit1.h" #include "math.h" #include "fixed_math.hpp" #include "algorithm.h" #define MBIT 0x4000 #define CBIT 16 long constbl; void __fastcall TForm1::Button1Click(TObject...
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...
0
by: techie88 | last post by:
Hello I'm completely new to C++ and VB programming. I am working on a Visual Studio 6 application that passes values from VB to C++ methods using BSTR conversions. Within the C++ code I am...
0
drhowarddrfine
by: drhowarddrfine | last post by:
Background: I have someone who uses Windows XP and software he bought for a cash register. He also has an online order form. When someone orders something online, we'd like for the XP box to...
3
oranoos3000
by: oranoos3000 | last post by:
hi i want to get time of server with ajax,php,javascript i use under scritp and with method onload i get time on server and show on the page so i want in a period time the same function is run...
8
Cintury
by: Cintury | last post by:
The problem is I have a function that I've created and stored in a module. I call it as an expression (e.g. total: Function(parameter)). I'm receiving the error 3061: too few parameters, expected 1....
2
by: Time | last post by:
Hi, In C,C++ and Java, while using printf()/cout/System.out.print()... if we call a function which returns some value, no error is generated; but if we call void type function; it gives an error....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.