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

  • Thread starter Peon666
  • Start date
  • Tags
    Push
In summary, the conversation discusses a problem with a piece of code that is causing an exception when run. The main concern is the for loop bounds, which seem to be letting the integer i range over the numbers from zero up through the number '\n', although this is likely not the intended behavior. The push() function is also being questioned, as it was copied from somewhere and may not be functioning properly. Other issues such as initialized variables and the number of items in the "elements" variable are also brought up as potential causes for the exception.
  • #1
Peon666
108
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
  • #2
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?
 
  • #3
End of line constant?
 
  • #4
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?
 
  • #5
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?
 

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

What is a "Push() problem with stack"?

A "Push() problem with stack" refers to an issue that occurs when using the push() method on a stack data structure. This method is used to add data to the top of the stack, but if the stack is full or there is an error in the code, the push() operation may not be successful, resulting in a "Push() problem with stack."

What causes a "Push() problem with stack"?

A "Push() problem with stack" can be caused by various factors, such as trying to push data onto a full stack, attempting to push data of an incompatible type, or encountering an error in the code that prevents the push() method from executing properly.

How can I fix a "Push() problem with stack"?

To fix a "Push() problem with stack," you will need to identify the cause of the issue. If the stack is full, you can either increase the size of the stack or remove some items from the stack before attempting to push new data. If the data type is incompatible, you will need to ensure that the data being pushed is of the correct type. If there is an error in the code, you will need to debug and fix the error before trying to push data again.

Can a "Push() problem with stack" be prevented?

Yes, a "Push() problem with stack" can be prevented by implementing proper error handling and boundary checks in the code. This can help prevent the stack from becoming full or pushing incompatible data, and can also catch any errors that may occur during the push() operation.

How does a "Push() problem with stack" impact the rest of the program?

A "Push() problem with stack" can impact the rest of the program by causing unexpected results or errors. If the push() operation is not successful, the data may not be added to the stack as intended, which can lead to incorrect output or unexpected behavior in the program. It is important to address and fix any "Push() problem with stack" to ensure the program runs correctly.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
9K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
13K
Back
Top