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

  • Thread starter Thread starter Peon666
  • Start date Start date
  • Tags Tags
    Push
AI Thread Summary
The discussion centers on a coding issue related to an exception thrown by the push() function. The primary concern is the for loop's bounds, where the variable 'i' is incorrectly allowed to range up to the newline character ('\n'), which may lead to unintended behavior. Additionally, there are doubts about the initialization of 'mystack' and the size of 'elements', which could also contribute to the problem. The push() function itself appears to be implemented correctly, but issues with its arguments or the stack's state may be causing the exception. Clarification on the type of 'mystack', the definition of 'array', and the specific exception details is needed for further diagnosis.
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?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top