What Is Causing the Exception in My Push() Function Code?

  • Thread starter Thread starter Peon666
  • Start date Start date
  • Tags Tags
    Push
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
Peon666
Messages
107
Reaction score
0
What's wrong with this piece of code:

Code:
for (i=0; i!='\n'; i++)
    {
        if (isdigit(elements[i]))
        {
            array[i] = elements[i] - '0';
            push(mystack, array);
        }
}

Whever I run it, it gives exception. There's some problem in this line but I can't igure what is it:

Code:
push(mystack, array);

Here's the implementation of the push function:

Code:
int push (stackADT stack, stack_elem_t elem)
{
    static int count;
    if (count == MAX_STACK_DEPTH-1)
    {
       stack->stk = realloc(stack->stk,2*sizeof(struct stackCDT) );
        MAX_STACK_DEPTH = 2*sizeof(struct stackCDT);
    }
    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);
}

What's the problem?
 
Physics news on Phys.org
There are a couple things that worry me, but the most significant one is your for loop bounds. You're letting the integer i range over the numbers from zero up through the number '\n'? '\n' is indeed a number (13, I think), but that's surely not what you intended?
 
End of line constant?
 
I did run the code without the statement:

Code:
push(mystack, array);

and it didn't generate any exception. So i guess the problem exists in this line?
 
TheUmer said:
I did run the code without the statement:

Code:
push(mystack, array);

and it didn't generate any exception. So i guess the problem exists in this line?

More likely the problem is actually occurring in the push() function. I do not see anything wrong with the push() function (which it looks as if he copied from somewhere anyway). Perhaps something is wrong with the arguments to push(), causing push() to misbehave? Is it possible mystack needs to be (but has not been) initialized. I'd also expect this code to crash anytime elements is less than 13 items.

Peon of the Beast: What was the exception? On what line did the exception occur? What is the type of mystack? What is "array"? And what is it you think this code is doing?