A little problem with Stacks in C

  • Thread starter Thread starter Peon666
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a problem encountered while implementing a Reverse Polish Notation (RPN) calculator in C, specifically focusing on issues related to stack operations such as pushing and popping elements. Participants explore the behavior of the code and the underlying stack implementation.

Discussion Character

  • Technical explanation
  • Debugging assistance
  • Exploratory reasoning

Main Points Raised

  • The original poster describes issues with summing integers and retrieving values from the stack, noting that incorrect values are printed.
  • Some participants request the definitions of the push and pop functions to better understand the problem.
  • One participant suggests using a debugger to identify the discrepancies between expected and actual behavior of the code.
  • Another participant points out that the values assigned to array2 from the pop function may represent error codes rather than the expected stack values.
  • There is a suggestion that the ADT_NOERROR value is zero and that other values indicate errors, leading to the hypothesis that the stack may not be functioning correctly.
  • The original poster acknowledges missing the stack creation step in the main program, which could be causing the issues.
  • Further discussion includes the need for proper pointer management and the implications of using dynamic memory allocation functions like alloc() and realloc().

Areas of Agreement / Disagreement

Participants generally agree that the stack implementation has issues, particularly related to the creation and management of the stack. However, specific details about the root cause and the best approach to resolve the issues remain contested.

Contextual Notes

Participants mention the importance of correctly initializing the stack and managing memory, but there are unresolved questions about the implementation of dynamic memory allocation and its integration into the existing code.

Who May Find This Useful

This discussion may be useful for individuals working on stack implementations in C, particularly those facing debugging challenges or exploring dynamic memory management in their programs.

Peon666
Messages
107
Reaction score
0
Alright guys, I was implementing RPN calculator in C language and during that, I have a little problem with the summing and poping thing. Here's how it goes:

My code is here:

case '+':
if (i < 2)
printf ("Not enough operators to perform addition operation!\n");
array2[0] = pop(mystack, &array[0]); // Output: 1
array2[1] = pop(mystack, &array[1]); // Output: 1
printf ("%d\n", array[0]); // Output: 3 (as I entered)
printf ("%d\n", array[1]); // Output: 4 (as I entered)
temp = (array[0]+array[1]);
push (mystack, temp);
printf ("%d\n", temp); // Output: 1
temp2 = pop (mystack, &temp);
printf ("%d\n",temp2); // Output: 1
break;

Now the problem is that this does not add the two integers. Why is this happening? Besides, it does not take anything in array2[0] and array2[1]. When I pop the elements array[0] and array[1] into array2[0] and array2[1] and print them out, only 1 is printed out. What could be the problem?

Further, when I add array[0] and array[1] into temp and print temp out, again, 1 is printed and nothing else. However, if I print out array[0] and array[1] separately, they are displayed as I enter them, i.e 3 and 4. What's going on? I'm really troubled about this fiasco! :(
 
Technology news on Phys.org
Show pop & push.
 
Instead of asking us to debug your code (and you have not shown push or pop), use the debugger. Learn how to use the debugger. It will help you understand why your code works the way it does, which is often quite different from how you think it should work.
 
Here are the push and pop functions:

Code:
int push (stackADT stack, stack_elem_t elem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == (MAX_STACK_DEPTH - 1)) {
        return (ADT_STACK_FULL);
    }
    ++stack->stack_ptr;
    stack->stk[stack->stack_ptr] = elem;
    return (ADT_NOERROR);
}

int pop (stackADT stack, stack_elem_t *pelem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == -1) {
        return (ADT_STACK_EMPTY);
    }
    *pelem = stack->stk[stack->stack_ptr];
    --stack->stack_ptr;
    return (ADT_NOERROR);
}

What now? And I do have problems dealing with the debugger. :(
 
Those array2 values you obtained when calling pop are not the values from the stack. They are the status from calling pop: One of ADT_INVALID_STACK, ADT_STACK_FULL, and ADT_NOERROR. What are these enum values?

I suspect that ADT_NOERROR is zero and the others are non-zero. If that is the case, your array2 values indicate that pop did not work. Something is wrong with your stack.
 
What could be that something? Should I post other piece of the code as well?
 
I'll post my entire code. See what and where the problem exists:

(I'm posting it all other than the MAIN programme which I've already posted)

Code:
#include <stdio.h>
#include <stdlib.h>

#define  MAX_STACK_DEPTH  10

enum stackADT_enums {
    ADT_NOERROR,
    ADT_INVALID_STACK,
    ADT_NOMEM,
    ADT_STACK_FULL,
    ADT_STACK_EMPTY,
};


typedef int stack_elem_t;
typedef struct stackCDT *stackADT;

struct stackCDT {
    stack_elem_t stk[MAX_STACK_DEPTH];
    int stack_ptr;
};

int create_stack (stackADT *pstack);
int free_stack (stackADT stack);
int push (stackADT stack, stack_elem_t elem);
int pop (stackADT stack, stack_elem_t *pelem);
int depth (stackADT stack);
int print_stack (stackADT stack);


int create_stack (stackADT *pstack)
{
    *pstack = malloc(sizeof(struct stackCDT));
    if (!*pstack) {
        return (ADT_NOMEM);
    }
    (*pstack)->stack_ptr = -1;
    return (ADT_NOERROR);
}

int free_stack (stackADT stack)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    free(stack);
    return (ADT_NOERROR);
}

int push (stackADT stack, stack_elem_t elem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == (MAX_STACK_DEPTH - 1)) {
        return (ADT_STACK_FULL);
    }
    ++stack->stack_ptr;
    stack->stk[stack->stack_ptr] = elem;
    return (ADT_NOERROR);
}

int pop (stackADT stack, stack_elem_t *pelem)
{
    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    if (stack->stack_ptr == -1) {
        return (ADT_STACK_EMPTY);
    }
    *pelem = stack->stk[stack->stack_ptr];
    --stack->stack_ptr;
    return (ADT_NOERROR);
}

int print_stack (stackADT stack)
{
    int i;

    if (!stack) {
        return (ADT_INVALID_STACK);
    }
    printf("Top ");
    for (i = stack->stack_ptr ; i >= 0 ; --i) {
        printf("%d, ", stack->stk[i]);
    }
    printf("Bottom\n");
    return (ADT_NOERROR);
}

void print_menu (void)
{
    printf("\nPress:\n");
    printf("1 to create stack.\n");
    printf("2 to free stack.\n");
    printf("3 to push element.\n");
    printf("4 to pop element.\n");
    printf("5 to print stack contents.\n");

    return;
}

What now?
 
You have a stack error. Look at your own results:

array2[0] = pop(mystack, &array[0]); // Output: 1

That output value of one means you had an ADT_INVALID_STACK error.
 
I just figured I didn't even include the create_stack thing in the main programe. That was pretty dumb, I know. Let me see if it works well after creating a stack. Hopefully it would.
 
  • #10
It looks like you're missing a lot of pointers
 
  • #11
Peon666: Try the following. Let us know what you get for the third and fourth outputs, below. If you get 7 for the third output, but 1 for the fourth output, wouldn't that mean push is causing a problem (probably because you omitted create_stack, as you mentioned)?

printf("%d\n",array[0]); // output: 3 (as I entered)
printf("%d\n",array[1]); // output: 4 (as I entered)
temp=array[0]+array[1];
printf("temp=%d\n",temp); // output: temp=
push(mystack,temp);
printf("temp=%d\n",temp); // output: temp=
 
  • #12
Alright, after including the create_stack in the main code, the problem has been reduced considerably. Push and Pop is working pretty fine, so are the operations of addition, subtraction etc.

Now, I have a further query. I want to use alloc() and then realloc() in the code. (Supposing that we initially enter five elements and then we decide for more elements so we use realloc()).

How could this be done? and would this be done in the main programe or in the fuction create_stack?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
Replies
47
Views
5K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K