Thread: How do I get more information about result of GDB output?

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    How do I get more information about result of GDB output?

    Greetings-
    I typed in the following lines of code, which is the answer to one of the questions in Let us C. I think the code was meant for a turbo compiler, and I am using gcc. So I got a core dumped message when I ran it. And then I used GDB to see if I can fix it. Here is the output:

    Code:
    Reading symbols from Dc...done.
    (gdb) break 2
    Breakpoint 1 at 0x80484af: file Dc.c, line 2.
    (gdb) run
    Starting program: /home/ghostrider/programing/Dc 
    
    Breakpoint 1, main () at Dc.c:3
    3    {
    (gdb) c
    Continuing.
    
    Program received signal SIGSEGV, Segmentation fault.
    0x08048548 in main () at Dc.c:16
    16             num[j] = 0;
    So there is something wrong in line 16. Right? But what? Nothing stands out to me from looking at this. I did Google the error, and did not find anything.

    Here is the entire code:

    Code:
    #include <stdio.h>
    main ()
    {
      int num[100], i, j, k, step;
    
      for ( i=0; i<= 99; i++)
        num[i] = i+1;
    
      for (i=1; i<=99; i++)
      {
        if (num[i] != 0)
         {
           k = num [i] * 2 -1;
           step = num[i];
           for (i=k; j<=99; j=j+step )
             num[j] = 0;
         }
      }
      printf ("\nPrime number between 1 & 100 are \n");
    
      for (i = 0; i<=99; i++)
      {
          if (num[i] != 0)
              printf("%d\t", num[i]);
      }
      printf ("\n\n\n\n\n\n\nThe end ....");
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It's not a syntax error, so it won't necessarily "look" wrong. It's a runtime error, a segmentation fault. So look at that line. What might be going on there that would cause an out-of-bounds error? At that point in gdb you would normally type "p j" in order to print the contents of variable j. Presumably it's 100 or more, which is outside of the array.

    BTW, the problem has nothing to do with different compilers. It's a logic error in the program.

    And you should explicitly have main return an int.

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by algorism View Post
    It's not a syntax error, so it won't necessarily "look" wrong. It's a runtime error, a segmentation fault. So look at that line. What might be going on there that would cause an out-of-bounds error? At that point in gdb you would normally type "p j" in order to print the contents of variable j. Presumably it's 100 or more, which is outside of the array.

    BTW, the problem has nothing to do with different compilers. It's a logic error in the program.

    And you should explicitly have main return an int.
    Thanks. You are right; setting j to 0 initially fixed my error, and ok I'll add the int before main().
    Here is the updated code and the output:

    Code:
    #include <stdio.h>
    main ()
    {
      int num[100], i, j, k, step;
    
      for ( i=0; i<= 99; i++)
        num[i] = i+1;
    
      for (i=1; i<=99; i++)
      {
        if (num[i] != 0)
         {
           k = num [i] * 2 -1;
           step = num[i];
           for (i=k; j<=99; j=j+step )
             num[j] = 0;
         }
      }
      printf ("\nPrime number between 1 & 100 are \n");
    
      for (i = 0; i<=99; i++)
      {
          if (num[i] != 0)
              printf("%d\t", num[i]);
      }
      printf ("\n\n\n\n\n\n\nThe end ....");
    }

    Prime number between 1 & 100 are
    2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Not very prime, are they. Your main error is here (look closely) :
    Code:
         for (i=k; j<=99; j=j+step)
    Also, don't say i <= 99. Use the common C idiom, i < 100.
    And use a define for the size of the array and use it in your loops.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You need to initialize your variables. The compiler does not initialize them, by default, so their value is undefined.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by algorism View Post
    Not very prime, are they. Your main error is here (look closely) :
    Code:
         for (i=k; j<=99; j=j+step)
    Also, don't say i <= 99. Use the common C idiom, i < 100.
    And use a define for the size of the array and use it in your loops.
    Got it:-) Thank you so much.

    Code:
    #include <stdio.h>
    #define arraySize  100 
    int main ()
    {
      int num[arraySize], i, j = 0, k, step;
    
      for ( i=0; i < 100; i++)
        num[i] = i+1;
    
      for (i=1; i < 100; i++)
      {
        if (num[i] != 0)
         {
           k = num [i] * 2 -1;
           printf ("\nHere is num[i]  -->%d\n",num[i]);
           step = num[i];
           for (j=k; j < 100; j=j+step )
             num[j] = 0;
         }
      }
      printf ("\nPrime number between 1 & 100 are \n");
    
      for (i = 0; i<=99; i++)
      {
          if (num[i] != 0)
              printf("%d\t", num[i]);
      }
      printf ("\n\n\n\n\n\n\nThe end ....");
    }

    Prime number between 1 & 100 are
    1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-26-2015, 11:04 PM
  2. Can't output a decimal number from a function result
    By samwillc in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2013, 01:59 PM
  3. Different Output Result
    By edmund085 in forum C Programming
    Replies: 7
    Last Post: 07-29-2011, 08:21 PM
  4. output screen isn't giving proper result
    By time4f5 in forum C Programming
    Replies: 11
    Last Post: 03-22-2011, 01:34 AM
  5. Replies: 3
    Last Post: 01-13-2002, 08:07 AM

Tags for this Thread