ram usage

Hi everybody,

I'm new in arduino and I have to ask some questions about ram usage.

I have to populate a defined structure with a name and a size, I thought to use a switch/case to insert into it the right values. The problem is that every single case will use some more RAM and I'm running out of memory. Here the example:

typedef struct
{
      char * name;
      int size ;

}  record;

record test;

void pop(int a)
{
   switch(a) {
     case 1:
         test.name = "test1";
         break;
     case 2:
         test.name = "test2";
         break;
     case 3:
         test.name = "test3";
         break;
   };
}

I'm doing something wrong? If I have 100 cases the ram will be drained a lot!

I saw something about the PROGMEM usage, but I didn't find a way to use it in this case.

Any suggestion?

Regards,

john

Your snippet doesn't make it clear what it is you're trying to do. It may well be that use of PROGMEM is the way for you to go, and you may need to rethink your solution.

You might give each test a number instead of a name, and then you could use a pointer to EEPROM to pull out a name string if necessary. You can write a seperate sketch to populate the EEPROM

I found a way to resolve the program with something like the F() macro. Using PSTR() macro and an apposite strcpy_P function. This solution permit me to save about 1K of RAM.

typedef struct
{
      char name[20];
      int size ;

}  record;

record test;

void pop(int a)
{
   switch(a) {
     case 1:
         strcpy_P(test.name,PSTR("test1"));
         break;
     case 2:
         strcpy_P(test.name,PSTR("test2"));
         break;
     case 3:
         strcpy_P(test.name,PSTR("test3"));
         break;
   };
}

Reference: http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

Thank you everybody.