Thread: Debugging GDB

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    Debugging GDB

    Hello!

    I am learning debugging with gdb. I have found this video on youtube
    http://www.youtube.com/watch?v=bAZTd769y8E

    Following the example, I am trying to run debugger, but it doesn't display a for loop.
    These are my steps from the Terminal:
    Code:
    (gdb) l
    10        d = -1;
    11        
    12        for (; d <= (TOTAL_ELEMENTS - 2); d++)
    13            printf("%d\n", array [d + 1]);
    14        
    15        return 0;
    16    }
    (gdb) break main
    Note: breakpoint 1 (disabled) also set at pc 0x100000e74.
    Breakpoint 2 at 0x100000e74: file debug1.c, line 7.
    (gdb) r
    Starting program: /Users/Alla/CS50/TextBook/kandr/exercises/debug1 
    
    Breakpoint 2, main () at debug1.c:7
    7        int array[] = {23, 34, 12, 17, 204, 99, 16};
    (gdb) n
    10        d = -1;
    (gdb) n
    15        return 0;
    (gdb)
    Here is the intentionally buggy program (example from youtube
    video):
    Code:
    #include <stdio.h>
    
    #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
    
    int main(void)
    {
        int array[] = {23, 34, 12, 17, 204, 99, 16};
        
        int d;
        d = -1;
        
        for (; d <= (TOTAL_ELEMENTS - 2); d++)
            printf("%d\n", array [d + 1]);
        
        return 0;
    }
    Please, help me to understand why I get to line 15 return 0 instead
    of line 12 which is a for loop?
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The type of result of sizeof is size_t, an unsigned integer type. Hence in the expression d <= (TOTAL_ELEMENTS - 2), (TOTAL_ELEMENTS - 2) is of type size_t because 2 is converted to size_t, assuming that the rank of size_t is greater than or equal to the rank of int, hence the entire expression is of type size_t because d is likewise converted to size_t. Since d == -1, it is converted to a large unsigned integer, and this unsigned integer is greater than (TOTAL_ELEMENTS - 2).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by laserlight View Post
    The type of result of sizeof is size_t, an unsigned integer type. Hence in the expression d <= (TOTAL_ELEMENTS - 2), (TOTAL_ELEMENTS - 2) is of type size_t because 2 is converted to size_t, assuming that the rank of size_t is greater than or equal to the rank of int, hence the entire expression is of type size_t because d is likewise converted to size_t. Since d == -1, it is converted to a large unsigned integer, and this unsigned integer is greater than (TOTAL_ELEMENTS - 2).
    Thank you very much for this helpful explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with debugging
    By Wiggles in forum C Programming
    Replies: 4
    Last Post: 07-21-2010, 05:22 AM
  2. Need help debugging...
    By n2d33p88 in forum C++ Programming
    Replies: 8
    Last Post: 12-13-2007, 04:15 AM
  3. Debugging help
    By iitb.ankit in forum C Programming
    Replies: 4
    Last Post: 06-04-2006, 07:20 AM
  4. debugging
    By Ssfccsh in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2004, 12:20 AM
  5. VC++ Debugging
    By neandrake in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2003, 07:31 PM

Tags for this Thread