[off topic]Strange Behaviour -- adding printf change the prograexecution

R

raxit

Hi


i just simply adding few debug like below and it changes program
execution ??

printf ("\n DEBUG %s:%d",__FILE__,LINE__); fflush(stdout);

i know its strange, and may be somewhere bug, but any good way to
debug.

PS: i have yet not attatched to gdb !!!


-Raxit
 
S

swengineer001

Hi

i just simply adding few debug like below and it changes program
execution ??

printf ("\n DEBUG %s:%d",__FILE__,LINE__); fflush(stdout);

i know its strange, and may be somewhere bug, but any good way to
debug.

PS: i have yet not attatched to gdb   !!!

-Raxit

I would guess you are doing something multithreaded and you just
screwed with the timing by adding the printf. This is usually an
indication of a failure to properly synchronize your threads.
 
J

jameskuyper

Hi


i just simply adding few debug like below and it changes program
execution ??

printf ("\n DEBUG %s:%d",__FILE__,LINE__); fflush(stdout);

i know its strange, and may be somewhere bug, but any good way to
debug.

This is not unusual. Your printf() call will, of course, cause
additional output, and a corresponding delay in the execution of other
parts of the code. If you're doing any kind of real-time processing,
that delay could, in itself, cause the behavior of those other parts
of your program to change.

However, if the behavior of your program is not time dependent, and
otherwise has well-defined behavior, then adding the printf() call
should only cause additional output to be created.

The problem is, if you've got a bug in your code, it has a pretty good
chance of making the behavior of the code undefined. When that's the
case, the actual behavior you observe will often change depending upon
minor details in the rest of your code. If, for instance, your program
is reading from or writing to a piece of memory that it shouldn't be,
the exact consquences will usually depend upon what's actually sitting
at that memory location. If it's a location your program can't read or
write, the program will probably abort with some kind of message from
your operating system telling you so. However, if it's a location your
program does have permission to read/write, but it happens to be the
wrong location, you're unlikely to see any such message; at least, not
until much later in the program.
The key point is that adding the printf() call can change what it is
that's in that memory location.

When you have this kind of problem, your best bet is to simplify the
program as much as possible. Remove some feature from the program that
you think shouldn't matter, and test it. If it still fails, remove
something more. If it succeeds, restore what you removed and remove
something else. Keep doing this until your program is as simple as
possible, without removing the failure.

In my experience, there's a very good chance that you will figure out
what's wrong just as a side effect of carrying out this simplification
process. However, if that doesn't happen, then I recommend posting the
simplified version of your program to this newsgroup, along with a
description of what you expected it to do, and the exact details of
what it actually did. Don't copy anything by hand - sometime defects
disappear when you re-type the source code, because you unthinkingly
correct the typo that is actually causing the program. Other times,
the hand copy introduces new defects that aren't actually in the
source code, making it harder to detect the real defect. Use copy-and-
paste to put the source code for your program and the output from the
program into your message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top