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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top