Thread: Weird stack problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    Weird stack problem

    My stack.h file :

    Code:
    /* stack using array implementation */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define max 5
    
    struct stack {
            int array[max];
            int top;
    };
    
    typedef struct stack stack;
    
    int isFull (stack *s);
    int isEmpty (stack *s);
    void push (stack *s, int value);
    void pop (stack *s);
    void printStack (stack *s);
    My stack.c file :

    Code:
    /* stack operations */
    
    #include "stack.h"
    
    int isFull (stack *s) {
            if (s->top >= max) {
                    printf("s->top is greater than or equal to max.\n");
                    return 0;
            } else {
                    return 1;
            }
    }
    
    int isEmpty (stack *s) {
            if (s->top == -1) {
                    return 0;
            } else {
                    return 1;
            }
    }
    
    void push (stack *s, int value) {
            if (!(isFull(s))) {
                    printf("Stack overflow.\n");
            } else {
                    s->top++;
                    s->array[s->top] = value;
            }
    }
    
    void pop (stack *s) {
            if (!(isEmpty(s))) {
                    printf("The stack is empty.\n");
            } else {
                    printf("The value being popped: %d\n", s->array[s->top--]);
            }
    }
    
    void printStack (stack *s) {
            int i;
            if (s->top == -1) {
                    printf("There is nothing to print.\n");
            } else {
                    printf("s->top : %d\n", s->top);
                    printf("Stack:\n");
                    printf("======\n");
                    for (i=s->top; i>=0; i--) {
                            printf("%d\n", s->array[i]);
                    }
            }
    }
    my stackMain.c file :

    Code:
    /* main file for stack */
    
    #include "stack.h"
    
    int main () {
            stack *ptr;
    
            ptr = (stack *) malloc (sizeof(stack));
            ptr->top = -1;
    
            push(ptr, 1);
            push(ptr, 5);
            push(ptr, 8);
            push(ptr, 11);
            push(ptr, 224);
            push(ptr, 9);
            pop(ptr);
    
            printStack(ptr);
            return 0;
    }

    After compilation, the result is:


    The value being popped: 0
    s->top : 8
    Stack:
    ======
    0
    0
    135137
    8
    224
    11
    8
    5
    1


    which is really weird, because I have limited the stack to five elements(or I thought I have) (in stack.h, #define max 5). However, the number of stack elements will change according to the last value I pushed onto the stack, in this case, it's 9, i.e. if I change the value of max to 100, the stack will grow to 100 elements.

    What did I do wrong?

    Thanks.

    Regards,
    barramundi9

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    May I suggest that if you name a function isX, then if X is the case, you return 1, otherwise you return 0? Because when I read !(isFull(s)), I think "not is full", but actually it means "is full", which is terribly confusing. If you really do want to return 0 to mean success, then use error codes instead of a boolean return value.
    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

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    37
    Thanks for the suggestion, laserlight. Actually, that was what I did at first, but the output was weird, so I changed it.

    Now I've changed to return 1 when it's full or empty and 0 when it's not, the output is still the same, any idea?

    Thanks.

    barramundi9

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are not counting correctly when the stack is full. It should be fairly obvious that in the push function you always take the else branch, otherwise you wouldn't be seeing this behaviour. Try stepping through your code on paper with a pencil. I suspect the problem is that you have max=5 elements in the stack but the last one is array[4] not array[5], since counting starts at 0. When you add the first element top becomes 0, ... when you add the 5th element top becomes 4. Since 4 is not >= max (which is 5) you keep adding.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    37
    OMG, you're right. It is fairly obvious ... my mistake. Once I change the if statement to if ((s->top) >= (max-1)), it works, thanks, claudiu.

    Any idea as to why the stack always grew to the last value pushed onto the stack before I corrected the if statement? if I pushed 9 to the stack, the stack grew to 9 elements, if I pushed 11, the stack grew to 11 elements. Any ideas?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your implementation is writing to stack[max], which is out of bounds. This explains why the stack "grows" to the last value you pushed, because it was actually (apparently) written over top.

    Other points:

    max should be all uppercase, and a better name would be STACK_MAX.

    Don't include stdio.h and stdlib.h in stack.h. Include them in stack.c instead.

    You aren't freeing the memory you malloc. Actually, you should have a newStack and deleteStack function in your stack implementation.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    37
    After drawing on paper, I see what you mean now. Thank you, oogabooga and everyone.

    Regards,
    barramundi9
    Last edited by barramundi9; 03-20-2012 at 01:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Really weird stack smashing problem
    By 68656c70 in forum C Programming
    Replies: 6
    Last Post: 07-23-2010, 06:50 PM
  2. Weird stack problem...
    By BC2210 in forum C Programming
    Replies: 2
    Last Post: 11-16-2008, 05:22 PM
  3. weird stack output
    By vidioholic in forum C Programming
    Replies: 27
    Last Post: 10-22-2008, 03:43 PM
  4. Weird access violation error with stack
    By ryeguy in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 02:53 AM