LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 856|回复: 10

这个错我真是想不明白啊

[复制链接]
发表于 2005-9-10 19:31:43 | 显示全部楼层 |阅读模式
源代码是这样:
     temp = list->num_node;
        printf( "number of nodes: %d\n", temp);

有错,调试:
(gdb) n
194             temp = list->num_node;
(gdb) print list->num_node
$1 = 4
(gdb) n
195             printf( "number of nodes: %d\n", temp);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.


list->num_node是int型,我觉得我正常给它赋值了,
' (gdb) print list->num_node
$1 = 4 '
也可以说明这点。
可是printf( "number of nodes: %d\n", temp); 报出seq fault,可能的原因是什么呢?
:ask
发表于 2005-9-10 19:45:11 | 显示全部楼层
能否给出更多的代码?
现在能想到的原因只是你把%d写成%n了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-10 20:03:42 | 显示全部楼层
Post by rickxbx
能否给出更多的代码?
现在能想到的原因只是你把%d写成%n了

不会吧,楼顶帖的代码是复制来的,如果写错了这里也能看得出来,hoho


  1. typedef struct node
  2. {
  3.         void* element;
  4.         struct node* next;
  5. } node_t;

  6. typedef struct
  7. {
  8.         node_t* head;
  9.         node_t* rear;
  10.         int num_node;//how many nodes
  11. } field_list_t;


  12. int main()
  13. {
  14.         field_list_t* list;
  15.         node_t* ptr;
  16.         node_t* first;
  17.         node_t* second;
  18.         node_t* third;
  19.         node_t* fourth;
  20.         node_init(&first);
  21.         node_init(&second);
  22.         node_init(&fourth);
  23.         int a = 1, b = 2, c = 3, d = 4;
  24.         int temp;
  25.         first->element = &a;
  26.         second->element = &b;
  27.         third->element = &c;
  28.         fourth->element = &d;
  29.         list_init(&list);
  30.         list_add( list, first, 0);
  31.         list_add( list, second, 0);
  32.         list_add( list, third, 0);
  33.         list_add( list, fourth, 3);
  34. /****************/
  35.         temp = list->num_node;
  36.         printf( "number of nodes: %d\n", temp);
  37. /*****************/
  38.         ptr = list->head;
  39.         temp = * (int*)ptr->element;
  40.         printf( "first node in the list is known as : %d\n", temp );
  41.         ptr = ptr->next;
  42.         temp = * (int*)ptr->element;
  43.         printf( "second node in the list is known as : %d\n", temp );
  44.         ptr = ptr->next;
  45.         temp = * (int*)ptr->element;
  46.         printf( "third node in the list is known as : %d\n", temp );
  47. }
复制代码


我把代码贴到这里时发现了出错的原因,hoho。让大家作为游戏找找吧。
不过,我还是很奇怪,temp既然有值,为什么seg fault出在printf那句呢?
回复 支持 反对

使用道具 举报

发表于 2005-9-10 20:17:07 | 显示全部楼层
还真看不出这里有什么错,小声请教一下
回复 支持 反对

使用道具 举报

发表于 2005-9-10 20:24:26 | 显示全部楼层
晕,third 没初始化~~~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-10 20:24:46 | 显示全部楼层
Post by rickxbx
还真看不出这里有什么错,小声请教一下

hoho, 也许怪我没有提供那些init函数的源码。
不过,从那连续的*init也可以推测,有对first的,对second的,对fourth的,却没有对third的,是不是可疑?hoho。再者,还没有malloc,这指针可不能拿出来就用啊,malloc封在那些init里了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-10 20:28:52 | 显示全部楼层
Post by rickxbx
晕,third 没初始化~~~~

应该把这个主题进行mutex_lock,等我回复完再mutex_unlock,你看,互斥问题解决不好,我的答复没有写完,就被你先回复了。

另外,我在第3帖的疑问,哪位仁兄有高见?
回复 支持 反对

使用道具 举报

发表于 2005-9-10 20:37:16 | 显示全部楼层
Post by Illidan
应该把这个主题进行mutex_lock,等我回复完再mutex_unlock,你看,互斥问题解决不好,我的答复没有写完,就被你先回复了。


^_^

另外,我在第3帖的疑问,哪位仁兄有高见?


可以让他产生core文件,然后debug这个core文件,可以确切的看到在哪条汇编语句处出的错.
回复 支持 反对

使用道具 举报

发表于 2005-9-11 14:13:04 | 显示全部楼层
Post by Illidan
不会吧,楼顶帖的代码是复制来的,如果写错了这里也能看得出来,hoho


  1. typedef struct node
  2. {
  3.         void* element;
  4.         struct node* next;
  5. } node_t;

  6. typedef struct
  7. {
  8.         node_t* head;
  9.         node_t* rear;
  10.         int num_node;//how many nodes
  11. } field_list_t;


  12. int main()
  13. {
  14.         field_list_t* list;
  15.         node_t* ptr;
  16.         node_t* first;
  17.         node_t* second;
  18.         node_t* third;
  19.         node_t* fourth;
  20.         node_init(&first);
  21.         node_init(&second);
  22.         node_init(&fourth);
  23.         int a = 1, b = 2, c = 3, d = 4;
  24.         int temp;
  25.         first->element = &a;
  26.         second->element = &b;
  27.         third->element = &c;
  28.         fourth->element = &d;
  29.         list_init(&list);/*是不是这句有问题,list已经是指针了,还取它地址干什么?很少见到这样的用法。一般好像是定义field_list_t list,然后list_init(&list)初始化一个空的链表的头结点*/
  30.         list_add( list, first, 0);
  31.         list_add( list, second, 0);
  32.         list_add( list, third, 0);
  33.         list_add( list, fourth, 3);
  34. /****************/
  35.         temp = list->num_node;
  36.         printf( "number of nodes: %d\n", temp);
  37. /*****************/
  38.         ptr = list->head;
  39.         temp = * (int*)ptr->element;
  40.         printf( "first node in the list is known as : %d\n", temp );
  41.         ptr = ptr->next;
  42.         temp = * (int*)ptr->element;
  43.         printf( "second node in the list is known as : %d\n", temp );
  44.         ptr = ptr->next;
  45.         temp = * (int*)ptr->element;
  46.         printf( "third node in the list is known as : %d\n", temp );
  47. }
复制代码


我把代码贴到这里时发现了出错的原因,hoho。让大家作为游戏找找吧。
不过,我还是很奇怪,temp既然有值,为什么seg fault出在printf那句呢?

field_list_t* list;
list_init(&list);/*是不是这句有问题,list已经是指针了,还取它地址干什么?很少见到这样的用法。一般好像是定义field_list_t list,然后list_init(&list)初始化一个空的链表的头结点*/
回复 支持 反对

使用道具 举报

发表于 2005-9-11 14:19:55 | 显示全部楼层
为什么对 fourth 调用 list_add 时使用 3 呀?能不能贴一下 list_add 的代码?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表