I have an C++ application program that is producing incorrect results
but the error goes away when I run it through the debugger.
I'm using the gcc/g++ compiler under Cygwin (an UNIX emulator that
runs on windows).
When I used the -ggdb compiler flag so I can run it under the gdb
debugger, the error goes away. It also runs fine if I do both of the
following; extract a few of the input instances which generate
incorrect results and put them in a separate input file.
Use this input file and recompile the program with no optimization
flags (I am otherwise using the -O3 optimization flag).
The -O3 optimizations are supposed to always produce the same
results. Running through the debugger is not supposed to change the
results either. Has anybody else encountered this behavior?
[The usual problem is that you're doing something that isn't valid
C, so the optimizer doesn't preserve its semantics. -John]
|
|
0
|
|
|
|
Reply
|
rhoads (1)
|
3/26/2011 7:35:22 PM |
|
When I was working, and people came to me with this sort of
problem, I developed a conditioned reflex to ask, "Are you sure you're
not using an uninitialised variable somewhere?". I'm not saying that it
definitely *is* this (our esteemed moderator points out that it could be
anything not valid in C); but it's well worth checking IME.
Hope this is useful!
Mark
|
|
0
|
|
|
|
Reply
|
Mark
|
3/27/2011 10:52:26 AM
|
|
rhoads@cs.rutgers.edu <rhoads@cs.rutgers.edu>
>I have an C++ application program that is producing incorrect results
>but the error goes away when I run it through the debugger.
A typical cause of such behaviour is that something in the executable
is being destroyed. Modifying the code in some way such as removing
or adding a print statement, etc, alters the effect.
Likely causes are a subscript error, or string error,
but is not restricted to those; pointer errors could be the cause.
|
|
0
|
|
|
|
Reply
|
robin
|
3/27/2011 3:01:23 PM
|
|
"rhoads@cs.rutgers.edu" <rhoads@cs.rutgers.edu> writes:
> The -O3 optimizations are supposed to always produce the same
> results.
No, that's not true. Optimization should preserve the semantics.
But the semantics often allow more than one result.
If you do something that is "undefined behavior" in C,
the semantics allow more than one result. In fact, an
infinite number of results. So turning on optimizations
can easily change which one of those results actually
happens at run time.
>... Running through the debugger is not supposed to change the
> results either.
Gcc and gdb go to a lot of trouble to make sure the debugger
doesn't perturb the results. But it's impossible to do that
100%. For example, stopping at a breakpoint changes the timing
of things, which can cause a multi-threaded program to behave
differently. Is your program multi-threaded by any chance?
Or handling interrupts/signals?
>... Has anybody else encountered this behavior?
It's fairly common that turning on optimizations changes results.
Solution: Use a language with fewer cases of "undefined behavior".
It's uncommon for debugging to change results.
But I've seen it happen.
Solution: print-out statements. (Those can change results, too,
if you're doing undefined behavior. But you might get lucky.)
- Bob
|
|
0
|
|
|
|
Reply
|
Robert
|
3/27/2011 5:09:53 PM
|
|
|
3 Replies
193 Views
(page loaded in 0.045 seconds)
Similiar Articles: Does PAUSE have any Side Effect ?? - comp.lang.fortranIf it goes away when you take ... > >> So get a debugger and run through the code ... of the same logic error. So you need good debugging tools like the Salford debugging compiler. problem with mixed c and fortran code - comp.lang.fortran ...To see where this goes for a given compiler, you ... view a disassembly > of compiler output ( :) ). ....in the case of gcc ... Compiler error, even though compiler ... removing /save fortran compiler option - comp.lang.fortran ...That way we have a baseline and can run two ... > > I was thinking of a perl script to go through and ... GNU Fortran: Using the compiler - GFortranUsage - GCC Wiki GNU Fortran ... thought: "Mini-x86"... - comp.lang.asm.x86problems then mostly go away... > Terje ... such as ret) would essentially have to go through a ... an undocumented requirement for special compiler options, e.g., gcc's ... improve strlen - comp.lang.asm.x86... write my pesonal routine faster than gcc compiler ... of the "fastest strlen" algorithm would go away. ... When you run 1,000,000 strings through your xstrlen function, you're ... Where did Fortran go? - comp.lang.fortran... but on their laptop they run gfortran. Tobias PS: By the way, even if many see GCC just as backup compiler: At ... if a user makes a error he wants to go back on ... Another class of "DO10I=" bug - comp.lang.fortranAs I provide debugging-help, I've run into array-assignment ... if it were for F90 you would have to wade through ... holding syntax making these sorts of issues go away ... IDE for ifort on Linux - comp.lang.fortran... has written an adaptation for its Fortran compiler and ... Linux and find it quite usable, once you get through ... than F90/95 syntax allows for even that to go away or ... Allocatable versus automatic arrays - comp.lang.fortranCode to Code, compiler to compiler, system to system. ... large arrays that must be kept during the whole run ... But as N goes to infinity, some claim that it doesn't ... FAQ -- assembly-language/x86/general/part1 - comp.lang.asm.x86 ...... Shareware A86 Assembler and D86 Debugger. The scope and content of this FAQ is to go ... Just thinking out loud as I go through ... The info shouldn't go away, it just ... DebuggingGCC - GCC Wiki - GCC, the GNU Compiler Collection - GNU ...... stepping through a front-end within a debugger ... to debug GCC and you run the debugger ... the Ada Compiler. If you run the debugger on ../../objdir/gnat1, you get an error ... GCC and G++ compiler error messages or run-time error messages.GCC and G++ compiler error messages or run-time error messages. ... ll note that the problem goes away if you ... assumes that you compiled with '-g' for debugging. MinGW | Minimalist GNU for WindowsCompiler system uses GCC to produce Windows programs. Win32 ports of GCC, GDB, binutils to build native Win32 programs that rely on no 3rd party DLLs. Website also ... c++ - I tried: valgrind, _GLIBCXX_DEBUG, -fno-strict-aliasing; how ...I have a really strange error that I've spend ... If I print &c[3], then the difference goes away. If I run on ... Browse other questions tagged c++ debugging gcc regression ... gcc - How Does The Debugging Option -g Change the Binary ...... on the compiler/linker. In the case of GCC, the option is -g. ... the debugging format originally designed to go along ... allows a debugger, such as gdb to step through ... 6/23/2012 4:18:44 AM
|