?
Solved

macro related C questions....

Posted on 2005-03-16
10
Medium Priority
?
282 Views
Last Modified: 2010-04-15
I have 3 questions.

(1) Is there any way to find out the name of the function that called a function.
For example,
function1 ()
{
      function2();
}

function2()
{
       // I want the name of function1 here.
}

Please dont tell me to set flags and check the flag or something...
I can know the function2 name from macro __func__, but I want name of function1

(2) Is it possible to set a macro to define a semi-colon. like...
#define { {printf("hai")

(3) How do I #define a empty statement

#define DEBUG 1
.
.......
#ifdef DEBUG
#define DFUN printf("debug statement")
#elseif
// ???? What should I write here??????????????
#endif

void main()
{
       DFUN;
}

As you can see here if set DEBUG to 0 I get error in main()
So what should I write in else part so that I do not get compiler error?

Also please explain your answers...

Thanks
venkat.
0
Comment
Question by:venkateshwarr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1080 total points
ID: 13562445
Hi venkateshwarr,

> (1) Is there any way to find out the name of the function that called a function.
> For example,
> function1 ()
> {
>       function2();
> }

> function2()
> {
>        // I want the name of function1 here.
> }

> Please dont tell me to set flags and check the flag or something...
> I can know the function2 name from macro __func__, but I want name of function1
Yes ... but it is very platform specific ... Unwind the function call stack manually .... A lot of work

> (2) Is it possible to set a macro to define a semi-colon. like...
> #define { {printf("hai")
Never tried it ... you should not be able to do it ... Macro names are supposed to follow the same rules are variable names if I remember correctly
 
> (3) How do I #define a empty statement

> #define DEBUG 1
> .
> .......
> #ifdef DEBUG
> #define DFUN printf("debug statement")
> #elseif
> // ???? What should I write here??????????????
> #endif

> void main()
> {
>        DFUN;
> }

> As you can see here if set DEBUG to 0 I get error in main()
> So what should I write in else part so that I do not get compiler error?
define a dummy variable
int dummy;
(void)dummy; in the else part should do the trick

Cheers!
sunnycoder
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13562472
1 - In Release mode - No
     In Debug mode Yes - but no straight forward way

2 - No

3 - Don't need to write any thing there.

Instead
#ifdef DEBUG
#define DFUN printf("debug statement")
#endif

void main()
{
#ifdef DEBUG      
DFUN;
#endif
}

_novi_
0
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13562649
Thanks for the replies,

Sunnycoder,

(1)Can you point give me some links where I can find more about  function call stacks?
(2) If I declare a dummy int/void variable. does that waste any CPU does that add any additional instruction to executable code... If yes, is there any other alternative??

_novi_,
I knew ur solution,  I dont want to write #ifdef statement at each any every time I want to write DFUN;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 1080 total points
ID: 13562684
>(1)Can you point give me some links where I can find more about  function call stacks?
Its specific to your platform ... For liunx this information is easily available in docs for gcc and gdb

>(2) If I declare a dummy int/void variable. does that waste any CPU does that add any additional instruction to executable
>code... If yes, is there any other alternative??
Declare the variable once ... globally ... this will take up few bytes ... and no run time cycles
Put (void)dummy; in the else part ... this will effectively do nothing and CPU cycles if consumed will be minimal
0
 
LVL 8

Assisted Solution

by:novitiate
novitiate earned 600 total points
ID: 13563256
>I dont want to write #ifdef statement at each any every time I want to write DFUN;

In that case

#ifdef _DEBUG
#define DFUN printf("debug statement")
#else
#define DFUN
#endif

void main()
{
   
DFUN;

}
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 200 total points
ID: 13566660
The easiest way is to call the function with a macro that passes the calling function name.

For example I often want to log where malloc is getting called from and for what size, like this:

#define malloc(size)     MyLoggingMalloc( size, __FUNCTION__ )



So your source file still has  malloc(...)   but what actually gets called has the function name as an extra parameter.

(2)  I don't know why you'd want to declare a dummy variable, but most every compiler will not add any extra code.  In fact most will optimize away the variable.    The only exception is if that's the only variable, and it didnt get optimized away,  there may be an extra instruction "sub sp,4"  to allocate stack space for just that variable.  

(3)  Just write  #define DFUN  
or #define DFUN   /* Nothing to see here... */
0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 120 total points
ID: 13571001
just another option
#ifdef _DEBUG
#define DFUN printf("debug statement")
#else
#define DFUN if(0)
#endif
0
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13580622
I was trying to know the function calls in a program using macros, in debug mode...

grg99,

(1) What if there is no malloc statement in a function??
 
(3) A simple

_asm { NOP; }
would do but I was looking for any other ideas...

I think novitiate solution is better..

shukla,

I think if(0) creates a jump statement to next location.
0
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13588346
I am closing the question for now, but if some one has any better ideas for question #2.. please post them... I will open a new question and give them points....

Anyway thanks for your comments..
venkat.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 13598519
#ifdef _DEBUG
#define DFUN printf("debug statement")
#else
#define DFUN 0 // or any number for that matter
#endif
==============================
novitiate solution looks best so far but I think you are looking for more intuitive approach. this may or may not compile. haven't tested.
#ifdef _DEBUG
#define DFUN printf("debug statement")
#else
#define DFUN {;}
#endif
==============================
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question