C中的分段错误(核心转储)运行时错误

时间:2013-10-24 00:11:20

标签: c segmentation-fault coredump

嗨,所以我是C的新手,刚刚用语言编写了我的第一个程序,并在尝试运行时遇到分段错误错误。我确信在整个代码中我犯了多个小错误。我已经完成了它,我无法弄清楚我的错误在哪里。这是我的代码:

// $Id: crpn.c,v 1.1 2013-10-22 13:28:04-07 - - $

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

int exit_status = EXIT_SUCCESS;
#define EMPTY (-1)
#define SIZE 16

typedef struct stack stack;
struct stack {
   int top;
   int capacity;
   int size;
   double numbers[SIZE];
};

void bad_operator (const char *oper) {
   fflush (NULL);
   fprintf (stderr, "oper=\"%s\"\n", oper);
   fflush (NULL);
   exit_status = EXIT_FAILURE;
   printf("%s: invaild operator\n", oper);
}

void push (stack *the_stack, double number) {
   if (the_stack->size == the_stack->capacity) {
        printf("%a:stack overflow", number);
    }
   else {
        the_stack->numbers[the_stack->size++]=number;
    }
}

void do_binop (stack *the_stack, char oper) {
  if ((the_stack->top)<1) {
        printf("oper=\"%c\":stack underflow\n", oper);
    }
   else {
        double right = the_stack->numbers[the_stack->size--];
        double left = the_stack->numbers[the_stack->size--];
        switch (oper) {
            case '+': push (the_stack, left + right); break;
            case '-': push (the_stack, left - right); break;
            case '*': push (the_stack, left * right); break;
            case '/': push (the_stack, left / right); break;
          }
        }
}

void do_print (stack *the_stack) {
   if (the_stack->top == -1) {
       printf("stack is empty\n");
    }
   else {
       int pos;
       for (pos = 0; pos <= the_stack->top; ++pos) {
          printf("%a\n",the_stack->numbers[pos]);
   }
 }
}

void do_clear (stack *the_stack) {
   the_stack->top = -1;
}

void do_operator (stack *the_stack, const char *oper) {
   switch (oper[0] ) {
         case '+': do_binop (the_stack, '+'); break;
         case '-': do_binop (the_stack, '-'); break;
         case '*': do_binop (the_stack, '*'); break;
         case '/': do_binop (the_stack, '/'); break;
         case ';': do_print (the_stack);      break;
         case '@': do_clear (the_stack);      break;
         default : bad_operator (oper);       break;
   }
}

int main (int argc, char **argv) {
   if (argc != 1) {
      fprintf (stderr, "Usage: %s\n", basename (argv[0]));
      fflush (NULL);
      exit (EXIT_FAILURE);
   }
   stack the_stack;
   the_stack.top = EMPTY;
   char buffer[1024];
   for (;;) {
      int scanrc = scanf ("%1023s", buffer);
      if (scanrc == EOF) break;
      assert (scanrc == 1);
      if (buffer[0] == '#') {
         scanrc = scanf ("%1023[^\n]", buffer);
         continue;
      }
      char *endptr;
      double number = strtod (buffer, &endptr);
      if (*endptr == '\0') {
         push (&the_stack, number);
      }else if (buffer[1] != '\0') {
         bad_operator (buffer);
      }else {
         do_operator (&the_stack, buffer);
      }
   }
   return exit_status;
}

3 个答案:

答案 0 :(得分:3)

让我“教你钓鱼”:

调试器会告诉您故障的确切位置。如果你使用的是IDE(Xcode,Eclipse,VS),它有一个很好的接口,你应该使用它。如果不是:

使用-g开关编译您的程序:gcc -g mycode.c。这会向可执行文件添加调试信息(使调试器为您提供更好的信息)。

$ gdb my_executable
...
> run
...
Segmentation fault
> where

这将为您提供确切的位置(哪个行号的功能)。

答案 1 :(得分:0)

您永远不会初始化the_stack.sizethe_stack.capacity

stack the_stack;
the_stack.top = EMPTY;
the_stack.size = 0;
the_stack.capacity = SIZE;

它还不清楚topsize之间的预期差异是什么。 push函数会增加size,但do_binopdo_print会使用永不递增的top

答案 2 :(得分:0)

问题在于,push()函数:

the_stack->numbers[the_stack->size++]=number;

你永远不会初始化你的结构的size成员,所以它只是随机的垃圾,而你正在走出数组的界限。它似乎也没有多大意义,因为你还有一个top成员,如果你这样做就不会更新。您似乎应该坚持使用topsize成员。