Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ignore printf

Reply
Thread Tools

ignore printf

 
 
iC and iC++
Guest
Posts: n/a
 
      12-17-2010
On Dec 17, 4:48*pm, pete <(E-Mail Removed)> wrote:
> iC and iC++ wrote:
>
> > On Dec 17, 2:25 pm, pete <(E-Mail Removed)> wrote:
> > > #ifdef DEBUG_ON
> > > #define MY_ASSERT(A) assert(A)
> > > #else
> > > #define MY_ASSERT(A)
> > > #endif

> > If I undef DEBUG_ON, what will the compiler do with MY_ASSERT.

>
> It makes MY_ASSERT be equal to no code.
>
> > I think *#define MY_ASSERT(A) should be declared outside the DEBUG_ON
> > if space.

>
> > In either case, DEBUG_ON is undefined, what will happen to the printf
> > statemetns, i.e. are they going to be ignored or the loop will break,
> > it seems like the loop will quit..

>
> Well, I wrote the program so that you could test run it easily
> once with DEBUG_ON defined,
> and then you could easily uncomment the
> * * #undef DEBUG_ON
> macro, and test run it again.
>
> By "easily",
> I mean that it is easier to do it and see what would happen
> than it is to wonder about it.
>
> --
> pete


Yeah, I should have. I caught my mistake as soon as I hit send.

Thanks for clarifying the code further. Someone brought up that this
would be wasting memory space, what are your thoughts on that. I have
enough memory to waste in this case, but I actually thought once the
code is compiled with optimization option, all the statements would be
ignored..
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      12-18-2010

"pete" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> BartC wrote:
>>
>> "iC and iC++" <(E-Mail Removed)> wrote in message

>
>> > Now, when I am done, i just want to be able to complile the program
>> > and take my hex file and load it onto my chip.

>
>> #ifdef DEBUG_ON
>> #define DPRINTF 1
>> #else
>> #define DPRINTF 0
>> #endif
>>
>> #define dprintf(...) if (DPRINTF) printf(__VA_ARGS__)
>>
>> ...
>>
>> dprintf("Testing %d, %d, %d\n",1,2,3);

>
> But iC and iC++ said
> "Now, when I am done,
> i just want to be able to complile the program
> and take my hex file and load it onto my chip."
>
> iC and iC++ might not want to waste program space on a bunch of
> if (0) printf(__VA_ARGS__)


#ifdef DEBUG_ON
#define dprintf(...) printf(__VA_ARGS__)
#else
#define dprintf(...)
#endif

dprintf("This a debugging message...\n");
printf("...but this is a real one...\n");

--
Bartc

 
Reply With Quote
 
 
 
 
Walter Banks
Guest
Posts: n/a
 
      12-18-2010


Ian Collins wrote:
>
> On 12/18/10 11:06 AM, Walter Banks wrote:
> >
> >
> > iC and iC++ wrote:
> >>
> >> Is there a way to make the compiler ignore the printf statements
> >> without me having to individually go and comment them out..
> >> like a macro of some kind..

> >
> >
> > There is an emotional thread "More on debuggers" running now
> > arguing for and against the use of printf's for debugging.
> >
> > Our customers for the most part need to ship what they debug
> > and there are times when they would like to log data during
> > test which means the code that is shipped needs to be
> > able to deal with unwanted print statements.
> >
> > What we did was to create a stdio library that is called exactly
> > like the normal stdio library functions except it creates
> > directives in our source level debugging information that
> > the debug environment can choose to use. These directives look
> > like predefined breakpoints with messages. The debug environment
> > interprets the embedded printfs in response to a breakpoint.
> > This means we have the ability to add printf statements to
> > environments that have no possibility of supporting them. The
> > first application that used this was a household thermostat.
> >
> > The second important aspect of this is there is no generated
> > code for a printf. We can ship the code that was tested without
> > the overhead of embedded printfs and we can use printfs to display
> > or log data during regression testing and debugging.

>
> That sounds very similar to Solaris dynamic tracing:
>
> http://www.sun.com/software/solaris/ds/dtrace.jsp
>
> Where probes in the code are only enabled when required. A great toll
> for analysing applications in production environment.


The difference is on the SUN dtrace is resolved by the same
processor that the code runs on and in our case there is no
generated code for the statements and the emulator or simulator
resolves the debug code

w..
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-18-2010
On Fri, 2010-12-17, Seebs wrote:
> On 2010-12-17, pete <(E-Mail Removed)> wrote:
>> But iC and iC++ said
>> "Now, when I am done,
>> i just want to be able to complile the program
>> and take my hex file and load it onto my chip."

>
>> iC and iC++ might not want to waste program space on a bunch of
>> if (0) printf(__VA_ARGS__)

>
> I would want to check, but it's not uncommon for compilers to
> optimize stuff like that away.


I would be shocked if a compiler (with the proper optimization level
turned on) /didn't/ optimize it away. It's obviously dead code.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Rich Webb
Guest
Posts: n/a
 
      12-18-2010
On Fri, 17 Dec 2010 20:25:49 -0500, pete <(E-Mail Removed)> wrote:

>iC and iC++ wrote:
>
>> Thanks for clarifying the code further. Someone brought up that this
>> would be wasting memory space, what are your thoughts on that. I have
>> enough memory to waste in this case, but I actually thought once the
>> code is compiled with optimization option, all the statements would be
>> ignored..

>
>In solution that I wrote, the MY_ASSERT code disappears
>from the translation unit.
>
>In the solution provided by Rich Webb, which I think I like
>a little bit better than what I came up with,
>the macro code also disappears from the translation unit.


The version that BartC shows, built around

#define dprintf(...) printf(__VA_ARGS__)

is probably the cleanest way to do it, but that capability was only
introduced in the C99 standard (although some compilers would have
supported it before that). The version I showed using the double
parentheses is rather uglier but should be universally available.

--
Rich Webb Norfolk, VA
 
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
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
Ignore argument to 'printf'? Arthur J. O'Dwyer C Programming 10 04-11-2005 05:50 AM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Ignore + TEST + Ignore SpooderStank Computer Support 2 04-08-2004 11:26 AM
Searching for Exact Phrase - should I ignore the ignore words? Rob Meade ASP General 6 03-01-2004 11:28 AM



Advertisments