Jeff Taylor wrote:
> I have a C program that GCC compiles without warnings and it runs just
> fine. However, when I compile it using any of the -O options (-O0, -O1,
> etc.) it segfaults when executed (still no compiler warnings or errors).
>
There are several approaches to this problem
1) The approach I usually take:
1.a Include the debug information when compiling (usually with -g)
1.b Do not strip the executable
1.c Start the gdb debugger and run your program within the debugger.
1.d When it crashes, take note of where it crashes. The specific
gdb command is "backtrace" as far as I remember.
1.e Look at the local variables. Note that the values displayed
by gdb are probably wrong since optimizations and debugging
do not mix well.
1.f Recompile the module where the crash happens WITHOUT
any optimizations
1.g Relink
1.h Rerun. Does the program crash? If no, you have found the
module where the fault is. Go to step 1.j
If yes, the fault is still there. Take another module from
the modules in the backtrace and recompile. Go to step 1.g.
1.i If you have recompiled all the modules in the backtrace and
the crash still persists, recompile one by one all other
modules until the crash disappears. The last module that
you recompiled is the module with the fault.
1.j Isolate the fault within the module. This can be tricky
unless there is a way to tell the compiler to enable/disable
optimizations in a function by function basis.
2) The approach recommended by the regulars in this group:
Read the source code. If you read hard enough the bug will be
obvious to you.
> I know nothing about compiler otimization, other than it makes my program
> run faster. Any suggestions on how to debug this, or what to look for
> would be greatly appreciated.
>
There is no free lunch. Optimized programs are more sensible to
programming errors than no optimized ones. Besides, you expose
yourself to the bugs of the optimizer, that are many.
If you are doing straight C though, it is highly unlikely that
there is a bug in the optimizer...
> Thank you.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32