Thread: Variable has bizarre and unexpected value

  1. #16
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    >> If you are on *nix/have access to gdb, check this thread:
    getting a segfault using pointers

    As far as this recommended thread/post #7 goes, it looks very relevant; however, I'm compiling my code using the 'make' command in Cygwin, because there are several .o files required to run my full system. I'm not sure how to make this debugging advice work, with my existing setup... and unfortunately, the debugger built in to the Visual C++ Express Edition that I'm using to edit the code, won't allow me to run or step through the code (maybe because I'm compiling in Cygwin)? Any suggestions about how I might practically debug are appreciated.

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't program on windows, but it looks like cygwin maybe includes gdb:

    Debugging Cygwin Programs

    Anyway, you can install gdb on windows. Try opening a terminal (the one that cygwin uses) and type "gdb -v".

    If it's there you're all set. I would say the first thing you want to do is set a breakpoint at this line:
    Code:
    nsteps = 200; /* simulate for a total of 2.0 sec */
    step = 0.010; /* time between samples */
    
    break #
    where # is the line number. Now:
    run
    when it gets to the breakpoint:
    print nsteps
    it will certainly be 200 at this point. After that just keep using "next" (debugging is often tedious) to proceed to the next instruction. If your linked objects do not have debugging symbols* compiled, you may descend into those function calls and get some ???, ignore this (probably this won't happen with "next", it only happens with "step"). Keep using print nsteps at each line of your own code** (you do not have to keep typing "next" and "print" -- just enter to repeat, and there is a history, so you can use the up arrow to go back to a previous command, so all you need to do is use the up arrow and enter).

    Eventually you'll find where nsteps value has mysteriously changed.

    * you do need to compile with debugging options on for your own code.
    ** you may want to skip loops by setting another break lower down and then use cont to go straight to the next breakpoint.
    Last edited by MK27; 06-11-2010 at 01:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #18
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    >> Anyway, you can install gdb on windows. Try opening a terminal (the one that cygwin uses) and type "gdb -v". If it's there you're all set.

    I checked, and it's not, but I'll look into installing gdb. Thanks for the info.

    _________________________

    >> Eventually you'll find where nsteps value has mysteriously changed.

    Update: by commenting out a section of code that I had suspected to be problematic, my screen output now indicates that every value of nsteps is back to the expected value of 200. So, once I correct that problematic code, that issue should be resolved.

    However, in the section of 4 function calls that I previously showed, when I print out the values of the returned variables, these are still sometimes-fine, sometimes-garbage values, so this is my current issue to debug. Which I will address, hopefully, using gdb.

Popular pages Recent additions subscribe to a feed