Thread: Overflow in Stack Counter

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Overflow in Stack Counter

    Program compiles successfully, as I am trying to implement the timeless postfix algorithm using a stack.

    It seems to, no matter what input I'm using, loop through 256 times until it encounters an overflow, as the stack is full.

    Where am I pushing on the elements 256 times?
    Code:
    #include <stdio.h>
    #define STACKSIZE 256
    #define TRUE 1
    #define FALSE 0
    
    /* stack type definition */
    
    typedef struct {
       int top;
       int items[STACKSIZE];
    } stack_type;
    
    /* function prototypes */
    void push(stack_type*, float); /* void, as nothing is returned */
    float pop(stack_type*);
    
    int main(void)
    {
       char buffer[256];
       char input[STACKSIZE];
       stack_type stk;
       int i=0;
       stk.top = -1;            /* Initialize the stack to empty */
    
       printf("This program will evaluate postfix expressions using a\n");
       printf("good implementation of stacks.\n");
       printf("You may enter up to a 50-char expression.\n");
       printf("Please enter a postfix/reverse-polish expression for me.\n");
       fgets(input, sizeof(input), stdin);
    
    
    while (1) {
    for (i=0; i<STACKSIZE; i++) {
    switch(input[i]) {
       case '+':
         push(&stk, (input[i] - 48));
         push((&stk), pop(&stk) + pop(&stk));
       break;
       case '-':
         push(&stk, input[i] - 48);
         push((&stk), pop(&stk) - pop(&stk));
       break;
       case '*':
         push(&stk, input[i] - 48);
         push((&stk), pop(&stk) * pop(&stk));
       break;
       case '/':
         push(&stk, input[i] - 48);
         push((&stk), pop(&stk) / pop(&stk));
       break;
       case '1':
         push(&stk, input[i] - 48);
       break;
       case '2':
         push(&stk, input[i] - 48);
       break;
       case '3':
         push(&stk, input[i] - 48);
       break;
       case '4':
         push(&stk, input[i] - 48);
       break;
       case '5':
         push(&stk, input[i] - 48);
       break;
       case '6':
         push(&stk, input[i] - 48);
       break;
       case '7':
         push(&stk, input[i] - 48);
       break;
       case '8':
         push(&stk, input[i] - 48);
       break;
       case '9':
         push(&stk, input[i] - 48);
       break;
       case '0':
         push(&stk, input[i] - 48);
       break;
       default:
         printf("Invalid Input\n"); }
    }
    }
    
    return 0;
    
    }
    
    /* IS Empty */
    float is_empty(stack_type* stk)
    {
       if ( (*stk).top == -1 )
          return TRUE;
       else
          return FALSE;
    }
    
    
    /* Push */
    void push(stack_type* stk, float element)
    {
       if ( stk == NULL )
       {  printf("Warning! Stack pointer is set to NULL.\n");
          exit(1); /* exit the program */
       }
    
       /* check stack is not full */
       if ( (*stk).top == (STACKSIZE - 1) )
       {  printf("Stack Overflow!\n");
          exit(1);
       }
    
       /* increment stack top */
       (*stk).top++;
    
       /* add the new element to the top */
       (*stk).items[(*stk).top] = element;
    
       return;
    }
    
    /* Pop */
    float pop(stack_type* stk)
    {  float value=0;
       /* Check stack exists */
       if ( stk == NULL )
       {  printf("Warning!  Stack pointer is set to NULL.  Is it really there?\n");
          exit(1);
       }
       /* check that the stack is not empty */
       if (is_empty(stk))
       {  printf("Stack underflow!\n");
          exit(1);
       }
       /* get top item */
       value = (*stk).items[(*stk).top];
       /* decrement top */
       (*stk).top--;
       /* return popped item to calling f(x); */
       return value;
    }
    Last edited by mlsrar; 09-30-2008 at 10:19 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Do you need two push statements in the +, -, *, / cases?
    2. Your while loop just repeats the for-loop ad infinitum (no new input, or anything like that, just the same processing over and over) until it dies.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Code:
    while(strcmp(input, "quit" != 0) {
    Removed duplicate push functions (bad copy/paste)...still same result? It loops through 256 times for some reason?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Removed duplicate push functions (bad copy/paste)...still same result? It loops through 256 times for some reason?
    What about the infinite loops that tabstop mentioned?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Changing the while condition is a start, but it's still all-or-nothing; if you get in the loop, you can't get out, since input doesn't change anywhere inside the loop.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Would it be legal to define a case statement to compare instead?

    case (strcmp(input, "quit" !=0)):
    printf("Goodbye!\n");
    return 0;

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. Case labels must be constants.

    And that still in no way gets more input from the user.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    OK, that's fair, and, here's my solution that now seg-faults (PS...any good tutorials on gdb?)

    Code:
      while (strcmp(input, 'quit' == 0)) {
    
       for (i=0; i<strlen(input); i++) {
       switch(input[i]) {
       case '+':
         /*push(&stk, (input[i] - 48));*/
         push((&stk), pop(&stk) + pop(&stk));
       break;
       case '-':
         /*push(&stk, (input[i] - 48));*/
         push((&stk), pop(&stk) - pop(&stk));
       break;
       case '*':
    ...
    ...
    ...
       case '0':
         push(&stk, (input[i] - 48));
       break;
       default:
         printf("Invalid Input\n"); }
    }
    }
    
    return 0;
    
    }
    It does compile with a warning of multi-character character constant, but seg-faults as soon as I have input. In theory, this should continue until quit is typed.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because you need to use "quit" instead of 'quit'.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    passing arg 2 of `strcmp' makes pointer from integer without a cast

    That's why I used the single quotes.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm, you get that warning because you use single quotes.
    Strings must always be double quotes. String literals are treated as const char*, that is, a proper argument to pass.
    You need to re-read some basic C, especially when it comes to pointers and strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mlsrar View Post
    passing arg 2 of `strcmp' makes pointer from integer without a cast

    That's why I used the single quotes.
    You get that warning BECAUSE you are using single quotes. If you use double quotes you wouldn't get either warning or the segfault.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If one were to ask me to define the term "Compiles fine" I would reply:

    Compiles fine - Adjective enhancing a verb. To compile without generating any errors or non-trivial warnings.

    So given the warnings that your code would have generated I would have to say it does not "compile fine." It simply just "compiles." Or I would also accept "compiles with warnings."

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Yes, I am learning C. In using double-quotes, I still receive the aforementioned warning during compilation and it segfaults regardless of input.

    I wouldn't think I could legally declare

    const char* input[STACKSIZE];

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can. It is an array of pointers to a constant block of data. So the pointer itself is not a constant value, just the object in which it references.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM