Thread: How to debug C program?

  1. #1
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    How to debug C program?

    Hi,
    Introductory information on debugging. How do I start to debug a progarm?
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    #define mydebug(x) perror( x ); fflush( stderr )

    Use it like:

    mydebug( "Just malloc'd some memory." );

    That's a fairly handy way to do it.
    Otherwise, consider something like the Microsoft debugger that comes with Visual Studios, or GDB which is on most *nix systems.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Sayeh
    Guest
    Well, here are some basics I wish more people in the industry followed. Debugging is the same, doesn't matter what language.


    Debugging starts with how your code is written in the first place--

    1) initialize your variables before use. This is particularly important where pointers are concerned.

    2) Dispose of pointers and memory allocations when through.

    3) Make sure things are balanced--

    If you have an open brace, make sure it has a close brace.

    If you allocate something, make sure you dispose of it

    If you enter a function, make sure you return from it

    etc.

    4) Check for errors

    If you allocate memory, or performing some action that might result in the O/S generating an error message-- CHECK FOR IT.

    If you get an error back from the O/S-- HANDLE IT. This is called being 'graceful'.

    5) Don't assume code is doing what you want. Step through it line by line and make sur eevery variable behaves the way you want. This is easy to do as you write sections of code.

    6) Use flow charts. Only amateurs write code "all in their heads" and don't use flow charts. A flowchart offloads your thinking onto paper so your mind isn't wasting effort on the "basics" of flow. It can concentrate on the intangible stuff.

    7) Learn how your processor handles your code and help the processor.

    Now, about actually debugging during execution:

    1) Set breakpoints and check the values and addresses of things to be sure they are proper.

    2) Monitor heap usage to be sure you don't have dangling pointers.

    3) Make sure your range-checking is working so ends of arrays and buffers aren't overrun.

    4) Isolate code-- this means that if something is breaking, keep cutting (using #ifdefs & ifndefs) code out so it doesn't execute. As soon as you cut something out and the problem goes away-- you've found the offending code.

    5) Use key-driven escapes... Let's say your code gets into a "loop" somewhere-- you'll know this happens because your program seems to "lock-up"... use isolation to find the offending problem, then insert a keyboard scanner that will terminate the program when a key is pressed-- This lets you bail gracefully, while still allowing you to examine processes and behaviors.

    Make sure everything you opened, you flushed and closed. Make sure everything you created was disposed of.

    Make sure you don't use or return invalid pointers. For example, if you have a local pointer variable in a function that is returned to a calling function-- you just created an invalid pointer because although the pointer returned is correct, the memory it points to was ont he stack and is destroyed when the called function exits.

    -----

    There are more, and more sophisticated means as well... This is just a start--

    enjoy.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Thanks guys.

    I didn't write the question, but it was something I wanted to ask as well.

    Muchas gracias.

  5. #5
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Arrow

    Hi Sayeh,
    Thanks for the tips.

    Your tips will help me to code more carefully. Why you are not a member of this board? Wish we will see you soon as a member.

    One thing I need to discuss. If I collect memory space using mallock, will it behave the same way?

    Ref:
    ----------------
    the memory it points to was ont he stack and is destroyed when the called function exits.
    -----------------
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM