Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: not calling function at run time .. (http://www.velocityreviews.com/forums/t313779-re-not-calling-function-at-run-time.html)

Mark A. Odell 06-24-2003 03:51 PM

Re: not calling function at run time ..
 
Paras Sharma <parsharm@cisco.com> wrote in
news:3EF870FF.1050102@cisco.com:

> I have one Library function DBG() - a kind wrapper kindof function
> for printf. ( it takes variable number of parameters )
>
> DBG( stringname, index, " Hello %d %s ..", i,c);
> DBG( stringname, index, " Hello %d %c %i %s ..", i,c1, j, c);


Variable arg count? Not possible in C89.

> I am using DBG at many places in the code . Now Just for testing I want
> not to call this function at run time.
> How to do it without commenting 10000s of places ..
>
> Is there any other like using some hashdefine #define DBG(ABC..)
> or some other way.


Yes.

Ex:

#if USE_DBG
# define DBG(x,y,z) do { x = y + z; } while (0)
#else
# define DBG(x,y,z) /* nothing */
#endif

Tailor to your needs.

--
- Mark ->
--

Zoran Cutura 06-24-2003 04:17 PM

Re: not calling function at run time ..
 
Mark A. Odell <nospam@embeddedfw.com> wrote:
> Paras Sharma <parsharm@cisco.com> wrote in
> news:3EF870FF.1050102@cisco.com:
>
>> I have one Library function DBG() - a kind wrapper kindof function
>> for printf. ( it takes variable number of parameters )
>>
>> DBG( stringname, index, " Hello %d %s ..", i,c);
>> DBG( stringname, index, " Hello %d %c %i %s ..", i,c1, j, c);

>
> Variable arg count? Not possible in C89.


Wasn't he talking about functions? You are probably into macros because
his DBG-function is named in upper case.

>
>> I am using DBG at many places in the code . Now Just for testing I want
>> not to call this function at run time.
>> How to do it without commenting 10000s of places ..
>>
>> Is there any other like using some hashdefine #define DBG(ABC..)
>> or some other way.

>
> Yes.
>
> Ex:
>
> #if USE_DBG
> # define DBG(x,y,z) do { x = y + z; } while (0)
> #else
> # define DBG(x,y,z) /* nothing */
> #endif
>
> Tailor to your needs.
>


Well that would do it for compile time. But for runtime he'll nee
something like:

#include <stdio.h>
#include <stdarg.h>

int nodbg;

void DBG(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);

vprintf(fmt, ap);

va_end(ap);

}

#define DBG(x) do { if (!nodbg) DBG x ; } while(0)

int main(int argc, char **argv)
{
if(argc > 1) nodbg = 1;

DBG(("talk %s\n", argc ? argv[0] : 0 ));

return 0;
}

With a C99 compiler (preprocessor) in place one can use _VA_ARGS_
instead of having to use the (()) notation with the macro.

--
Z (Zoran.Cutura@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond

Arthur J. O'Dwyer 06-24-2003 04:34 PM

Re: not calling function at run time ..
 

On Tue, 24 Jun 2003, Zoran Cutura wrote:
>
> Mark A. Odell <nospam@embeddedfw.com> wrote:
> > Paras Sharma <parsharm@cisco.com> wrote in
> > news:3EF870FF.1050102@cisco.com:
> >
> >> I have one Library function DBG() - a kind wrapper kindof function
> >> for printf. ( it takes variable number of parameters )
> >>
> >> DBG( stringname, index, " Hello %d %s ..", i,c);
> >> DBG( stringname, index, " Hello %d %c %i %s ..", i,c1, j, c);


> >> I am using DBG at many places in the code . Now Just for testing I want
> >> not to call this function at run time.
> >> How to do it without commenting 10000s of places ..
> >>
> >> Is there any other like using some hashdefine #define DBG(ABC..)
> >> or some other way.

>
> Well that would do it for compile time. But for runtime he'll nee
> something like:


[trimmed for brevity]
> int nodbg;
>
> void DBG(const char *fmt, ...)
> {
> va_list ap;
> va_start(ap, fmt);
> vprintf(fmt, ap);
> va_end(ap);
> }
>
> #define DBG(x) do { if (!nodbg) DBG x ; } while(0)
>
> int main(int argc, char **argv)
> {
> if(argc > 1) nodbg = 1;
> DBG(("talk %s\n", argc ? argv[0] : 0 ));
> return 0;
> }


Mightn't it work just to use a function pointer? I've done similar
things quite often. Still requires either double parentheses or a
dummy expression, so the code still has to change, but it removes
the need for a global 'nodbg' flag that should logically be tied to
the DBG function itself.

Untested code follows...

void REAL_DBG(const char *fmt, ...)
{ /* same thing */ }

void (*DBG)() = REAL_DBG;

int main(int argc, char **argv)
{
if (argc > 1) DBG = 0;
DBG? DBG("talk %s\n", argc? argv[0]: 0) : (void)0;
return 0;
}

-Arthur


Goran Larsson 06-24-2003 04:53 PM

Re: not calling function at run time ..
 
In article <Xns93A478AFECB6Dlkj562ghjgk1k245lbvj@130.133.1.4> ,
Mark A. Odell <nospam@embeddedfw.com> wrote:
> Paras Sharma <parsharm@cisco.com> wrote in
> news:3EF870FF.1050102@cisco.com:


> > I have one Library function DBG() - a kind wrapper kindof function
> > for printf. ( it takes variable number of parameters )
> >
> > DBG( stringname, index, " Hello %d %s ..", i,c);
> > DBG( stringname, index, " Hello %d %c %i %s ..", i,c1, j, c);


> Variable arg count? Not possible in C89.


Why do you think that it is not possible to have a function with a
variable number of arguments in C89?

> > I am using DBG at many places in the code . Now Just for testing I want
> > not to call this function at run time.
> > How to do it without commenting 10000s of places ..


Rename your DBG function and then change all uses of it to have double
parantheses, like this:

DBG(( stringname, index, " Hello %d %s ..", i,c));

Then define a macro calling your old DBG function as in this example:

---->8-------->8-------->8-------->8-------->8-------->8-------->8----
#include <stdio.h>

#define USE_DEBUG

#ifdef USE_DEBUG
# define DBG(args) printf args
#else
# define DBG(args)
#endif

int
main ( void )
{
DBG (( "debug is %s\n", "on" ));

return 0;
}
---->8-------->8-------->8-------->8-------->8-------->8-------->8----

--
Göran Larsson http://www.mitt-eget.com/

Mark A. Odell 06-24-2003 06:20 PM

Re: not calling function at run time ..
 
hoh@invalid.invalid (Goran Larsson) wrote in news:HGzw8F.Frt@approve.se:

>> > DBG( stringname, index, " Hello %d %s ..", i,c);
>> > DBG( stringname, index, " Hello %d %c %i %s ..", i,c1, j, c);

>
>> Variable arg count? Not possible in C89.

>
> Why do you think that it is not possible to have a function with a
> variable number of arguments in C89?


Due to the ALL CAPS I assumed these were macros, not functions.

--
- Mark ->
--


All times are GMT. The time now is 08:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.