Overflow in Stack Counter

This is a discussion on Overflow in Stack Counter within the C Programming forums, part of the General Programming Boards category; Program compiles successfully, as I am trying to implement the timeless postfix algorithm using a stack. It seems to, no ...

  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 11:19 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    22,271
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,185
    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,185
    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
    Posts
    23,013
    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,185
    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.

Page 1 of 3 123 LastLast
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, 04:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  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, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21