I am curious, if I declared 4 arrays, of type int, does the compiler allocate all memory needed to populate fully each array?
Or do I need to worry about using too much memory?
An example would be:
program uses say 70% of total available memory at initiation. Included in that is it deadspace for 0 value of the arrays? Or is it space needed to fully populate an all 0 array at startup?
memory % part is just for guessing - net result, is an array fully padded at initiation or does it grow in memory as values are stored?
narf - hope this makes sense
Thanks!!
The short answer is that the compiler does allocate the memory.
Memory for all static variables including arrays with no or partially initialized data are allocated when the system start up. Variables declared outside of a function are static.
Variables declared within a function have memory allocated from the memory stack at runtime unless the variable is explicitly declared as static. If insufficient memory is available for these stack variables, the system crashes.
It is possible to dynamically allocate memory using functions like malloc but this is very tricky because of the difficulty of handling the case where you need the memory but there is none left for you to get. IMHO using Malloc is asking for trouble.
So, if memory is tight, my advice is:
- think about moving constant data, particularly strings, into progmem
- use the smallest appropriate data type
- look closely at big arrays to see if you can find more efficient ways of managing that data.
Excellent, thanks tons Mem - that's exactly what I was looking for.