分段错误(核心转储)的C运行时错误 [英] Segmentation fault (core dumped) runtime error in C

查看:122
本文介绍了分段错误(核心转储)的C运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我新的C,只是写了我的第一个程序的语言和不断收到分段错误,当我尝试运行它。我敢肯定,有我在整个code由多个小失误。我已经通过它去,我不能揣摩出我的错误是。这里是我的code:

  // $编号:crpn.c,1.1版2013年10月22日13:28:04-07  -   -  $#包括LT&;&ASSERT.H GT;
#包括LT&;&libgen.h GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT EXIT_STATUS = EXIT_SUCCESS;
的#define EMPTY(-1)
#定义尺寸16typedef结构栈堆栈;
结构栈{
   INT榜首;
   INT能力;
   INT大小;
   双号[SIZE]
};无效bad_operator(为const char * OPER){
   fflush(NULL);
   fprintf中(标准错误,OPER = \\%s \\的\\ n,OPER);
   fflush(NULL);
   EXIT_STATUS = EXIT_FAILURE;
   的printf(%S:invaild运营商\\ n,OPER);
}无效的push(堆栈* the_stack,双号){
   如果(the_stack->大小== the_stack->能力){
        的printf(%A:堆栈溢出号);
    }
   其他{
        the_stack->数字[the_stack->大小++] =号;
    }
}无效do_binop(堆栈* the_stack,焦炭OPER){
  如果((the_stack->顶部)。1){
        的printf(OPER = \\%C \\:堆栈下溢\\ n,OPER);
    }
   其他{
        双右= the_stack->数字[the_stack-> size--]。
        双击左键= the_stack->数字[the_stack-> size--]。
        开关(OPER){
            案例'+':推(the_stack,+左右);打破;
            案例' - ':推(the_stack,左 - 右);打破;
            案例'*':推(the_stack,左*右);打破;
            案例'/':推(the_stack,左/右);打破;
          }
        }
}无效do_print(堆栈* the_stack){
   如果(the_stack->顶== -1){
       的printf(堆栈是空\\ n);
    }
   其他{
       INT POS;
       对于(POS = 0; POS&L​​T; = the_stack->顶++ POS){
          的printf(%A \\ N,the_stack->数字[POS]);
   }
 }
}无效do_clear(堆栈* the_stack){
   the_stack->顶= -1;
}无效do_operator(堆栈* the_stack,为const char * OPER){
   开关(OPER [0]){
         案例'+':do_binop(the_stack,'+');打破;
         案例' - ':do_binop(the_stack,' - ');打破;
         情况下*:do_binop(the_stack,'*');打破;
         案例'/':do_binop(the_stack,'/');打破;
         案;:do_print(the_stack);打破;
         案例'@':do_clear(the_stack);打破;
         默认:bad_operator(OPER);打破;
   }
}INT主(INT ARGC,字符** argv的){
   如果(argc个!= 1){
      fprintf中(标准错误,用法:%S \\ n,基名(的argv [0]));
      fflush(NULL);
      出口(EXIT_FAILURE);
   }
   堆叠the_stack;
   the_stack.top =空的;
   字符缓冲区[1024];
   为(;;){
      INT scanrc = scanf函数(%1023s,缓冲区);
      如果(scanrc == EOF)破;
      断言(scanrc == 1);
      如果(缓冲[0] =='#'){
         scanrc = scanf函数(%1023 [^ \\ n],缓冲区);
         继续;
      }
      字符* endptr;
      双数=的strtod(缓冲,&安培; endptr);
      如果(* endptr =='\\ 0'){
         推(安培; the_stack,号​​码);
      }否则如果(缓冲[1]!='\\ 0'){
         bad_operator(缓冲液);
      }其他{
         do_operator(安培; the_stack,缓冲区);
      }
   }
   返回EXIT_STATUS;
}


解决方案

让我教你以渔:

一个调试器会告诉你到底何处有故障。如果您使用的是IDE(X code,Eclipse中,VS),它有一个很好的接口之一,你应该使用。如果不是:

-g 开关编译程序: gcc的-g我的code.C 。这将调试信息添加到可执行文件(使调试器给你更好的信息)。

  $ GDB my_executable
...
>跑
...
分段故障
>哪里

这会给你的准确位置(这在其行号的功能)。

Hi so I am new to C and just wrote my first program in the language and keep getting the segmentation fault error when I try to run it. I'm sure there are multiple small mistakes I have made throughout the code. I have gone through it and I can't figure out where my mistake is. Here is my code:

// $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;
}

解决方案

Let me "teach you to fish":

A debugger will tell you exactly where the fault is. If you're using an IDE (Xcode, Eclipse, VS) it has a nice interface to one and you should use that. If not:

Compile your program with the -g switch: gcc -g mycode.c. This adds debugging information to the executable (makes the debugger give you much better info).

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

This will give you the exact location (which function on which line number).

这篇关于分段错误(核心转储)的C运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆