Is there a limit to the number of global variables used?

Status
Not open for further replies.

grease_lighting

Well-known member
I'm using Arduino 1.8.5 along with TeensyDuino 1.40.
I am using a Teensy 3.2. Currently the sketch is at 42% of program storage space and 18% dynamic memory.

I am having problems with my code running properly.
In particular it hangs at the tft.print statements when displaying a menu screen.
When it hangs at the tft.print, I must press the program load button to load a new sketch when debugging.
I'm trying to boil the code down to meet the forum rule 'post code example'.
I find that by deleting some variables I can add an equal amount more without encountering the error.
I also find that I can safely declare an array to a larger size without encountering the problem. After all I am not adding another variable.

So the question is. . . . Is there a limit to the number of global variables used?

Thanks.
 
So the question is. . . . Is there a limit to the number of global variables used?

Yes, but at 18% used, you're very unlikely to be anywhere near that limit.

Memory is used in 3 ways. How much of the remaining 82% is used by stack & heap is hard to know. But 82% free is quite a lot...

Heap usage is done with malloc() and C++ "new", and also with Arduino "String" (which internally uses malloc), and also any libraries you're using which use those features. Most libraries don't use that stuff though, so usually you can just look at your own code to see if you've used malloc or new.

Stack usage is done with the local variables in your functions, plus all the functions in libraries you're using. The big red flag to notice is any large array as a local variable. Most libraries avoid this, so again you can usually just focus on your own code. The other possible issue is any recursive functions (functions that call themselves). Even if your function uses only a few small local variables, if it calls itself hundreds of times, those hundreds of temporary copies of the variables can add up.


Now this isn't what you asked, but I can tell you the 2 really common cases we see where programs mysteriously hang are buffer overflows and unsafe use of interrupts. If you have any arrays and you use loops to write them, carefully check to make sure the last time the loop runs isn't writing beyond the end of the array. Corrupting "random" stuff in memory tends to cause very mysterious crashes.

Interrupts are much harder to check. Generally the best approach is to not use interrupts unless absolutely necessary. For timing, use things like elapsedMillis. If you absolutely must use an interrupt, try to limit it only to accessing as few volatile variables as possible. Try not to ever call other functions from an interrupt. Many functions and libraries are not designed to be safe to use from interrupts. Many things usually work, but do crash mysteriously if the interrupt occurs at just the wrong moment when other code is manipulating certain data.
 
Found it. I was referencing a part of an array during a read that was not valid.

Also found I was not manipulating some bits improperly too '&&' and '||' when I should have used '&' or '|'.

@Paul Thank you for your advice and help to enable me to find this subtle error.
 
Status
Not open for further replies.
Back
Top