LinuxSir.cn,穿越时空的Linuxsir!

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

为什么会出现段错误?

[复制链接]
发表于 2004-5-20 13:26:15 | 显示全部楼层 |阅读模式
该列队链接能正确的编译运行完,但在运行完之后出现一个"段错误",为什么?

  1. #include <stdio.h>

  2. typedef struct linked_queue
  3. {
  4.   int data;
  5.   struct linked_queue *next;
  6. } LqueueTp;

  7. typedef struct queueptr
  8. {
  9.   LqueueTp *front, *rear;
  10. } QueptrTp;

  11. int main()
  12. {
  13.   QueptrTp *lq;
  14.   int input;
  15.   int i = 0;
  16.   int outnum;

  17.   InitQueue(&lq);
  18.   printf("请输入一些数字:");
  19.   scanf("%d", &input);
  20.   while (input != 0)
  21.     {
  22.       EnQueue(&lq, input);
  23.       scanf("%d", &input);
  24.     }
  25.   GetHead(&lq, &input);
  26.   printf("当前链接列队的队头为:%d", input);
  27.   printf("\n请输入要出队的个数:");
  28.   scanf("%d", &outnum);
  29.   printf("依此出队的数为:");
  30.   while (i < outnum)
  31.     {
  32.       OutQueue(&lq, &input);
  33.       printf("%d ", input);
  34.       i++;
  35.     }
  36.   printf("\n当前列队排列:");
  37.   while (! EmptyQueue(&lq))
  38.     {
  39.       OutQueue(&lq, &input);
  40.       printf("%d ", input);
  41.     }
  42.   printf("\n\n");
  43. }

  44. int InitQueue (QueptrTp **lq)
  45. {
  46.   LqueueTp *p;
  47.   p = (LqueueTp *) malloc (sizeof(LqueueTp));
  48.   (*lq) -> front = p;
  49.   (*lq) -> rear = p;
  50.   ((*lq) -> front)  -> next = NULL;
  51. }

  52. int EnQueue (QueptrTp **lq, int x)
  53. {
  54.   LqueueTp *p;
  55.   p = (LqueueTp *) malloc (sizeof(LqueueTp));
  56.   p -> data = x;
  57.   p -> next = NULL;
  58.   ((*lq) -> rear) -> next = p;
  59.   (*lq) -> rear = p;
  60. }

  61. int OutQueue (QueptrTp **lq, int *x)
  62. {
  63.   LqueueTp *s;
  64.   if ((*lq) -> front == (*lq) -> rear)
  65.     {
  66.       printf("队空!\n");
  67.       return 0;
  68.     }
  69.   else
  70.     {
  71.     s = ((*lq) -> front) -> next;
  72.     *x = s -> data;
  73.     ((*lq) -> front) -> next = s -> next;
  74.     if (s -> next == NULL)
  75.       (*lq) -> rear = (*lq) -> front;
  76.     free(s);
  77.     return 1;
  78.     }
  79. }

  80. int EmptyQueue (QueptrTp **lq)
  81. {
  82.   if (((*lq) -> rear) == ((*lq) -> front))
  83.     return 1;
  84.   else
  85.     return 0;
  86. }

  87. int GetHead (QueptrTp **lq, int *x)
  88. {
  89.   LqueueTp *p;
  90.   if ((*lq) -> rear == (*lq) -> front)
  91.     return 0;
  92.   else
  93.     {
  94.       p = ((*lq) -> front ) -> next;
  95.       *x = p -> data;
  96.       return 1;
  97.     }
  98. }

复制代码
发表于 2004-5-20 13:27:44 | 显示全部楼层

先自己调试一下吧

出现段错误是因为访问了不能访问的内存空间。
发表于 2004-5-21 09:03:54 | 显示全部楼层
找找吧,内存,变量定义或使用的问题。等等
发表于 2004-5-21 10:01:44 | 显示全部楼层
i haven't compile your program,

but .... such code is dangerous, it may crash your program.
p = ((*lq) -> front ) -> next;

you should use the following code:
if( *lq )
  if( (*lq)->front )
     p = ((*lq) -> front ) -> next;
发表于 2004-5-21 11:55:59 | 显示全部楼层
QueptrTp *lq;
这个lq没初始化。
发表于 2004-5-21 11:58:52 | 显示全部楼层
另外:
InitQueue等函数的参数不必要使用QueptrTp **类型,
用QueptrTp * 就足够了
发表于 2004-5-21 15:39:22 | 显示全部楼层
段错误是lq没有初始化就使用造成的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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