Segmentation fault (core dumped) runtime error in C

浪尽此生 提交于 2019-12-11 09:52:38

问题


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

回答1:


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).




回答2:


You're never initializing the_stack.size and the_stack.capacity.

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

It's also not clear what the intended difference between top and size is. The push function increments size, but do_binop and do_print use top, which is never incremented.




回答3:


The problem is this, in your push() function:

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

You never initialize the size member of your struct, so it's just random garbage, and you're going way out of bounds on the array. It also doesn't seem to make a lot of sense, since you also have a top member, which won't be updated if you do this. Seems like you should stick to either a top or a size member.



来源:https://stackoverflow.com/questions/19554639/segmentation-fault-core-dumped-runtime-error-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!