On 2016-02-07, Pavan Maddamsetti <pavan.maddamse...@gmail.com> wrote: > Thanks for helping me with this issue, Mr. Guenther. I'm not familiar > with gdb so it may take some time for me to learn it...however, please > note that > > cc -O0 wtf.c -o wtf > > produces a working binary where > > cc -O2 wtf.c -o wtf > > causes the program to get stuck. So I have reason to believe GCC's > optimizations are faulty.
That is *sometimes* the case, but it usually just means that the program relies on something which is undefined behaviour; compilers are allowed to interpret this how they want, and often do, especially where the optimizer is concerned. Quick intro: compile with -g then "gdb ./wtf", "break main" (or some other function), "run", step with 'n', use 'print' to examine variables. Examine online help / docs for more. > I did attempt to initialize listp as NULL. However, the code then > dereferences listp with (*last)->next causing a segmentation fault. Didn't you say that it gets overwritten with either ap or bp though? If that's the case, how is it dereferencing a null pointer?