trying to debug a small program but cannot continue debugging after printf(), please help! this is the code:

#include <stdio.h>
#include <stdlib.h>

struct dll {
    struct dll* prev;
    int data;
    struct dll* next;
};
struct dll* head;

int main() {
    struct dll *p1, *p2, *p3, *p4, *p5, *temp;
    struct dll * add=NULL;
    p1 = (struct dll *)malloc(sizeof(struct dll));
    p2 = (struct dll *)malloc(sizeof(struct dll));
    p3 = (struct dll *)malloc(sizeof(struct dll));
    p4 = (struct dll *)malloc(sizeof(struct dll));
    p5 = (struct dll *)malloc(sizeof(struct dll));
    p1->prev = NULL;
    p1->data = 1;
    p1->next = p2;
    p2->prev = p1;
    p2->data = 2;
    p2->next = p3;
    p3->prev = p2;
    p3->data = 3;
    p3->next = p4;
    p4->prev = p3;
    p4->data = 4;
    p4->next = p5;
    p5->prev = p4;
    p5->data = 5;
    p5->next = NULL;
    head=p1;

    int count=0, pos=0,i=0;
    printf("add of p1::%p add of p2::%p add of p3::%p add of p4::%p add of p5::%p\n", p1, p2, p3, p4, p5);
    for ( temp = p1; temp != NULL; temp = temp->next ){
        count++;
    }
    if(count%2==0)
        puts("even nodes so no midlle node");
    else if(count%2!=0)
    {
        pos=count/2;
        printf("pos::%d\n",pos+1);
        for ( i=0; i<=pos; i++)

        {
            for ( temp = p1;temp != NULL;temp = temp->next )
            {

                if(i==pos)
                    printf("middle node is %p\n",temp);
            }
        }
    }
    return 0;
}

Debugging output:

(gdb) b 47
Breakpoint 1 at 0x8048643: file sony.c, line 47.
(gdb) b 57
Breakpoint 2 at 0x8048696: file sony.c, line 57.
(gdb) run
Starting program: /home/jeevan/Documents/jvt/ds/sony 
add of p1::0x804b008 add of p2::0x804b018 add of p3::0x804b028 add of p4::0x804b038 add of p5::0x804b048
pos::3

Breakpoint 1, main () at sony.c:47
47  for ( i=0; i<=pos; i++)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
47  for ( i=0; i<=pos; i++)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
47  for ( i=0; i<=pos; i++)
(gdb) s
50  for ( temp = p1;temp != NULL;temp = temp->next )
(gdb) s
53  if(i==pos)
(gdb) s
54  printf("middle node is %p\n",temp);
(gdb) s
__printf (format=0x804879d "middle node is %p\n") at printf.c:28
28  printf.c: No such file or directory.
(gdb) s
__x86.get_pc_thunk.bx () at ../sysdeps/i386/i686/multiarch/strcat.S:55
55  ../sysdeps/i386/i686/multiarch/strcat.S: No such file or directory.
(gdb) s
__printf (format=0x804879d "middle node is %p\n") at printf.c:32
32  printf.c: No such file or directory.
(gdb) s
33  in printf.c
(gdb) s
_IO_vfprintf_internal (s=0xb7fb3ac0 <_IO_2_1_stdout_>, format=format@entry=0x804879d "middle node is %p\n", 
    ap=ap@entry=0xbfffef44 "\b\260\004\b\030\260\004\b(\260\004\b8\260\004\bH\260\004\b\001") at vfprintf.c:235
235 vfprintf.c: No such file or directory.
(gdb) s
270 in vfprintf.c

After printf() I am stuck in a loop, I don’t understand what it is telling and how much time should I wait to go to next statement?

share|improve this question
1  
It looks fine to me. Does it run okay without the debugger? – wallyk Aug 30 '14 at 17:00
1  
It's trying to locate the source code for printf, but can't find it. So it's just stepping through instruction by instruction. – Oliver Charlesworth Aug 30 '14 at 17:01
3  
You probably want to use next instead of step to iterate through your statements in gdb. – Bill Lynch Aug 30 '14 at 17:02
    
Yes thanks Oli Charlesworth, I don't know about next, I am learning on my own! I missed next somehow! I normally use s or si or stepi. Thanks for your time! – jeevan Aug 30 '14 at 17:12
    
Yes thanks Sharth, I don't know about next, I am learning on my own! I missed next somehow! I normally use s or si or stepi. Thanks for your time! – jeevan Aug 30 '14 at 17:13
up vote 2 down vote accepted

It runs okay without the debugger for me. And using "next" instead of "step" is important: (Note I have reformatted your code for readability.)

[wally@lenovoR61 ~]$ gdb t
GNU gdb (GDB) Fedora (7.3.50.20110722-16.fc16)
Copyright (C) 2011 Free Software Foundation, Inc.
...
Reading symbols from /home/wally/t...done.
(gdb) start
Temporary breakpoint 1 at 0x804842d: file t.c, line 14.
Starting program: /home/wally/t 

Temporary breakpoint 1, main () at t.c:14
14      p1 = malloc(sizeof(struct dll));
(gdb) s
__GI___libc_malloc (bytes=12) at malloc.c:2914
2914    {
(gdb) 
2919        = force_reg (__malloc_hook);
(gdb) 
2920      if (__builtin_expect (hook != NULL, 0))
(gdb) 
2921        return (*hook)(bytes, RETURN_ADDRESS (0));
(gdb) 
malloc_hook_ini (sz=12, caller=0x8048439) at hooks.c:30
30  {
(gdb) 
31    __malloc_hook = NULL;
(gdb) 
32    ptmalloc_init();
(and further into obscurity)

But, with next:

[wally@lenovoR61 ~]$ gdb t
GNU gdb (GDB) Fedora (7.3.50.20110722-16.fc16)
Copyright (C) 2011 Free Software Foundation, Inc.
 ...
Reading symbols from /home/wally/t...done.
(gdb) start
Temporary breakpoint 1 at 0x804842d: file t.c, line 14.
Starting program: /home/wally/t 

Temporary breakpoint 1, main () at t.c:14
14      p1 = malloc(sizeof(struct dll));
(gdb) n
15      p2 = malloc(sizeof(struct dll));
(gdb) 
16      p3 = malloc(sizeof(struct dll));
(gdb) 
17      p4 = malloc(sizeof(struct dll));
(gdb) 
18      p5 = malloc(sizeof(struct dll));
(gdb) 
19      p1->prev = NULL;    p1->data = 1;   p1->next = p2;
(gdb) 
20      p2->prev = p1;      p2->data = 2;   p2->next = p3;
(gdb) 
21      p3->prev = p2;      p3->data = 3;   p3->next = p4;
(gdb) 
22      p4->prev = p3;      p4->data = 4;   p4->next = p5;
(gdb) 
23      p5->prev = p4;      p5->data = 5;   p5->next = NULL;
(gdb) 
24      head=p1;
(gdb) 
26      int count=0, pos=0, i=0;
(gdb) 
27      printf("add of p1::%p add of p2::%p add of p3::%p add of p4::%p add of p5::%p\n", p1, p2, p3, p4, p5);
(gdb) 
add of p1::0x804a008 add of p2::0x804a018 add of p3::0x804a028 add of p4::0x804a038 add of p5::0x804a048
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
29          count++;
(gdb) 
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
29          count++;
(gdb) 
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
29          count++;
(gdb) 
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
29          count++;
(gdb) 
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
29          count++;
(gdb) 
28      for (temp = p1; temp != NULL; temp = temp->next)
(gdb) 
31      if (count%2==0)
(gdb) 
35          pos=count/2;
(gdb) 
36          printf("pos::%d\n",pos+1);
(gdb) 
pos::3
37          for (i=0; i<=pos; i++)
(gdb) 
39              for (temp = p1;temp != NULL;temp = temp->next)
(gdb) 
41                  if (i==pos)
(gdb) 
39              for (temp = p1;temp != NULL;temp = temp->next)
(gdb)
  (and continues to work well)
share|improve this answer
    
Ha yes by using next it is fine! Thanks I don't know about next, I am learning on my own! I normally use s or si or stepi. Thanks a lot! – jeevan Aug 30 '14 at 17:11
2  
@jeevan: You are welcome. Andwelcome to S.O.! – wallyk Aug 30 '14 at 17:14
    
Oh thank you :) @wallyk – jeevan Sep 1 '14 at 9:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.