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

Program malfunctions when adding a new variable

Hi All,
First of all, thanks for reading this and going through my problem.
As I am not a pro (not even the half way), its a little difficult to solve all problems myself.
- I tried to dig as much as I can withing my limit of knowledge.
- I also tried searching for this issue with a few different keywords on Google and this forum.

Just a brief about my work and code.
I am making a Graphical LCD base oscilloscope with LPC2148 board lying with me.
Things were moving nicely and I finish through 75% of my work with waveform and some measurements.

Then, around 4-5 days back, to add one more measurement, I created 2 unsigned short in type variable (and later on other types as well). This translated in change of other functionality which were not related to it at all.

To check, where the problem is,
-I moved back to my last working code and checked it. That worked fine.
-I just added one variable and then checked, it worked. (as that variable was not used, I think intelligent compiler didn't translate it to hex code.)
-I added just one statement so that variable translate in to code. Statement was "x = x+1;"

This made my scope go made.

I tried many ways to check this but didn't get anything,

To give a little more details
-I am using around 300 short int and around 30-40 other variables. It would need around 1k of RAM. Remaining 7k is still free.
-I am using keil demo version. And my code size is ~8k.
-I am using USB bootloader.
-If I declare variable outside function (global) it is not giving any issue.

Last thing, I feel that this may be issue with stack. As all variables are in main(), it would be going out of room but not sure about anything. Going ahead without proper knowledge would not be a good idea (though issues is solved half the way).

-any pointers to other literature would be welcomed.

Thanks for reading through this point.
Thanks for help.

  • Yes, when you add more local variables, you need to be prepared to also make the stack (in the startup file) larger.

    Note that Keil can produce an html file that shows an analysis of expected stack needs - i.e. it tries to determine the different call trees and how much stack space that is needed.

    Auto (local) variables are best for local use.

    If you have a big buffer where you store your samples, then you should definitely consider making this into a global array. Potentially a static array to make it only visible within that specific source file - then you could add accessor functions add_sample(), ... to actually make use of the array.

    But anyway - when you get too much auto variables or too deep call nesting, the stack will overflow. And when the stack overflows, lots of interesting things can happen. You may get a total lockup. Or the program may just behave strangely because the execution gets send to an totally incorrect place in the code. Stack overflow also means that your variables can't be trusted anymore. Function calls can result in your variables being garbled, suddently holding normally impossible values.

  • Thank you very much Per.
    I appreciate your help for doubt clarification.

    Have a good day :)