This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

stack overflow

The following line of code seems to be causing inexplicable results

if((PulsePeriod != 0))

As it is, this "if" block is entered despite the variable "PulsePeriod" having been immediately previously set to 0. Removing the inner parentheses gives different results so I can only assume this is because of a stack overflow (I am assuming that the parenthesised items are pushed onto the stack and then popped off for the final evaluation). Is there anyway I can verify this?

This is test code and the parentheses are there because a second comarison is being Logical Anded with this one in the actual code and has been remove to simplify things.

  • "Removing the inner parentheses gives different results so I can only assume this is because of a stack overflow (I am assuming that the parenthesised items are pushed onto the stack and then popped off for the final evaluation). Is there anyway I can verify this?"

    I should think that unnecessary parenthesis are simply ignored by the compiler. I sounds more like you have a bug elsewhere in your code - bad pointer, overrunning array bounds etc. Unless you have used a lot of idata space for variables it is unlikely that you have a 'genuine' stack overflow - if the stack does wrap it is most likely a result of a bug.

    If you want to check the stack usage you could prefill it with a known value then check to see how much has been overwritten at some suitable point in your code, or slightly less usefully you could check to see whether SP has acquired a lower value than it started with at some suitable points.

  • A guess
    PulsePeriod is not a char
    PulsePeriod is written by an ISR

    Correct?

    Erik

  • It would be helpful to see the code snippet in context, along with the actual declarations of the PulsePeriod variable, and a description of other places it might be used. It also would help to post the assembler listing generated from this source.

  • I can only assume this is because of a stack overflow (I am assuming that the parenthesised items are pushed onto the stack and then popped off for the final evaluation).

    Why assume this? You can look at the assembler listing and KNOW. Compilers rarely push values on the stack for comparisons.

    Jon

  • pulse period is not a char (It is a ULONG data) and is written by an ISR, yes ... why do you ask?

  • I should also mention that the problem continues even if the ISR is disabled.