A few days ago I encountered memory problems (SRAM shortage) on my Arduino. I've read a lot and fixed the problem, now I have around 900 bytes free in SRAM. However, I still have difficulties understanding and using progmem
.
The user is allowed to create up to 10 programs (a program in my case is just some object storing the user's preferences). As I don't want to run out of SRAM, I planned to store them in PROGMEM
.
Consider the following class:
#define MAX_PROGRAMS 10
#define MAX_PREFS 10
class Program
{
public:
Program(String name, Preference* prefs[]); // ctor
String getName();
static byte getNoOfPrograms();
static Program* getProgram(byte idx);
private:
const String _name;
static int _programCtr;
Preference* _preferences[MAX_PREFS];
static Program* _programs[MAX_PROGRAMS];
};
One notices that a program can store up to 10 preferences, hence, if the user decides to make 10 programs, each with 10 different preferences, we will definitely run out of SRAM.
Therefore, can I store instances of the above Program
class in PROGMEM if I define them as below?
const Program* const pgm PROGMEM = new Program(F("Test"), ...);
// ...
delete pgm;
I presume this is possible since the whole object is const
and could never be changed. I actually tried this and had no compiler errors, however, I was stumbled by the fact that the above two lines of code use 22 bytes of SRAM, when I expected it not to use any.
In my case, would it be better not to store the whole object in progmem
but only its constant fields, and do the same for all constants in Preference
? (i.e. change const String _name
to const char name[] PROGMEM;
etc.)
progmem
is going to be fixed at compile time? I ask because you wish to useprogmem
to store user defined programs, so that makes sense only if the user defines that programs before compilation occurs... which to me makes the whole thing a nonsense... but I believe it all comes down to what is a program for you...program
instances will be created at runtime. Hence, i think i will have no other option than limiting the user to a few programs..String
class anymore in order not to fragment the heap. Now, what do you mean by tokenizing my program and storing it as binary code? :)