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

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

Discussion Overview

The discussion revolves around a piece of code that is generating an exception when executing a push function within a stack implementation. Participants are analyzing the for loop and the push function to identify potential issues, focusing on the bounds of the loop and the state of the stack.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the bounds of the for loop, suggesting that using '\n' as a limit may not be the intended behavior, as it corresponds to a numerical value (13).
  • Another participant speculates that the problem may lie within the push function, indicating that the arguments passed to it could be causing issues, particularly if mystack has not been initialized.
  • It is noted that running the code without the push statement does not produce an exception, implying that the issue is likely related to that specific line.
  • There is a request for clarification regarding the type of mystack, the nature of the exception, and the purpose of the array variable, indicating a need for more context about the code's functionality.

Areas of Agreement / Disagreement

Participants express differing views on where the problem may lie, with some focusing on the for loop and others on the push function. No consensus is reached regarding the exact cause of the exception.

Contextual Notes

There are uncertainties regarding the initialization of mystack, the size of elements, and the intended behavior of the code, which may affect the execution and lead to exceptions.

Who May Find This Useful

Readers interested in debugging stack implementations, understanding loop bounds in C programming, or those facing similar issues with exception handling in code may find this discussion relevant.

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?
 
Technology 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?
 

Similar threads

Replies
20
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
8
Views
4K
Replies
2
Views
2K
Replies
3
Views
4K