Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: not calling function at run time ..

Reply
Thread Tools

Re: not calling function at run time ..

 
 
Mark A. Odell
Guest
Posts: n/a
 
      06-24-2003
Paras Sharma <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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 ->
--
 
Reply With Quote
 
 
 
 
Zoran Cutura
Guest
Posts: n/a
 
      06-24-2003
Mark A. Odell <(E-Mail Removed)> wrote:
> Paras Sharma <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> 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 ((E-Mail Removed))
"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
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      06-24-2003

On Tue, 24 Jun 2003, Zoran Cutura wrote:
>
> Mark A. Odell <(E-Mail Removed)> wrote:
> > Paras Sharma <(E-Mail Removed)> wrote in
> > news:(E-Mail Removed):
> >
> >> 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

 
Reply With Quote
 
Goran Larsson
Guest
Posts: n/a
 
      06-24-2003
In article <Xns93A478AFECB6Dlkj562ghjgk1k245lbvj@130.133.1.4> ,
Mark A. Odell <(E-Mail Removed)> wrote:
> Paras Sharma <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):


> > 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/
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      06-24-2003
http://www.velocityreviews.com/forums/(E-Mail Removed)lid (Goran Larsson) wrote in news:(E-Mail Removed):

>> > 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 ->
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Re: not calling function at run time .. Derk Gwen C Programming 0 06-24-2003 08:45 PM
Re: not calling function at run time .. Wictory C Programming 0 06-24-2003 05:14 PM
not calling function at run time .. Paras Sharma C++ 1 06-24-2003 03:51 PM



Advertisments