尝试调试一个小程序但无法在printf()之后继续调试,请帮忙! 这是代码:
#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;
}
调试输出:
(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
在printf()之后我陷入了一个循环中,我不明白它在说什么,我应该等多久才能进入下一个声明?
答案 0 :(得分:2)
没有调试器,它运行正常。并使用&#34; next&#34;而不是&#34; step&#34;很重要:(注意我为了便于阅读而重新格式化了代码。)
[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)
但是,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)