Putting a struct in progmem

This is doing my head in.

I want to store a struct containing nine floats in progmem to free up the ram it would otherwise consume. I’ve followed the discussion on how to do it, all seem to end up placing an array in progmem, not a single struct, and this might be the core of the problem.

Here is some test code:

#include <avr/pgmspace.h>

struct Coeff
{
  float b0;
  float b1;
  float b2;
  float b3;
  float b4;
  float a1;
  float a2;
  float a3;
  float a4;  
};

// // This struct in flash
const Coeff C PROGMEM = {0.0048243,
                         0.0192974,
                         0.0289461,
                         0.0192974,
                         0.0048243,
                         2.3695159,
                        -2.3139877,
                         1.0546647,
                        -0.1873776};

// // This struct in RAM
// const Coeff C = {0.0048243,
//                  0.0192974,
//                  0.0289461,
//                  0.0192974,
//                  0.0048243,
//                  2.3695159,
//                 -2.3139877,
//                  1.0546647,
//                 -0.1873776};

void setup()
{
  Serial.begin(9600);
  delay(1000);
  // Read struct from progmem and print values
  Serial.println(pgm_read_float(&C.b0),8);
  Serial.println(pgm_read_float(&C.b1),8);
  Serial.println(pgm_read_float(&C.b2),8);
  Serial.println(pgm_read_float(&C.b3),8);
  Serial.println(pgm_read_float(&C.b4),8);
  Serial.println(pgm_read_float(&C.a1),8);
  Serial.println(pgm_read_float(&C.a2),8);
  Serial.println(pgm_read_float(&C.a3),8);
  Serial.println(pgm_read_float(&C.a4),8);

  // // Read struct from RAM and print values
  // Serial.println(C.b0,8);
  // Serial.println(C.b1,8);
  // Serial.println(C.b2,8);
  // Serial.println(C.b3,8);
  // Serial.println(C.b4,8);
  // Serial.println(C.a1,8);
  // Serial.println(C.a2,8);
  // Serial.println(C.a3,8);
  // Serial.println(C.a4,8);
}

void loop() 
{
  // put your main code here, to run repeatedly:

}

I run the code first with the struct in progmem, then with it in RAM (by commenting out and in the relevant code) and here are the compile results:

Struct in progmem
Sketch uses 3314 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for local variables. Maximum is 2048 bytes.

Struct in RAM uses 72 bytes less flash but still 200 bytes of RAM
Sketch uses 3242 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for local variables. Maximum is 2048 bytes.

The flash usage changes by 72 bytes (why not 36?), between the two runs, but the RAM usage does not. It seems that the compiler puts the struct in both memory spaces. How can I get it to release the space used in RAM?

Pogo

I would turn the problem on its head and look at why you need to store such numbers , especially when used to operate against say and analog input. .
When any mathematical operation is used you can loose a lot of precision , so the precision of you floats becomes meaningless .
If you need decimal places you can often work in integer arithmetic , using values of say thousanths of your value and recreate the decimal places in your output .
You can also use a “ bigger “ processor .

Is it possible that the compiler has worked out that each of the 9 values is a constant, because they never get changed, and optimised-away the struct completely?

Thanks @hammy The example is just to show the fault. My program uses a display which eats a lot of the ram and couple of serial ports which use more, so memory is tight. How tight? Increasing the font size causes a crash. I want to claw back some RAM, and putting constants into flash is a good solution

That’s very possible @PaulRB, but as it has used the flash to store the values in, how is storing them in RAM as well an optimisation?

You are expecting that flash memory usage will be reduced by
36 bytes (9 float x 4 = 36); but, you are getting a reduction by 72 bytes which is good for you. However, you see an arithmetic mismatch here!

I would like to see the flash use increase by 36 bytes and the RAM use decrease by 36 bytes. And that is not happening.

And taking a hint from my mention of arrays, I rewrote the const struct as a single element array, and got the same result, so it isn’t anything to do with the difference between a struct and an array.

The compilar messages are:

Variable     Flash          RAM        Flash usage          RAM usage
struct       x              ------     3314                 200
struct       -----          x          3242                 200
volatile     -----          x          3350                 236

Now, you see that RAM usage has increased by 36 bytes which you have wanted. With volatile keyword, you are telling the compiler to store the struct into RAM memory (which is a volatile memroy); otherwise, the compiler keeps the struct into the registers of the MCU as a part of optimization #3 @PaulRB.

volatile const Coeff C = 
                {
                  0.0048243,
                  0.0192974,
                  0.0289461,
                  0.0192974,
                  0.0048243,
                  2.3695159,
                 -2.3139877,
                  1.0546647,
                 -0.1873776
                };

I think that you have my problem the wrong way round @GolamMostafa , I’m trying to reduce RAM use. The best place for constants is in flash. The problem is that putting the struct there doesn’t decrease RAM usage by the same amount.

When you use PROGMEM keyword, are you agree that the struct goes into flash memory and there is no usage of RAM? If yes, then you are saving RAM space.

The fact that I have wanted to establish in #9 that the struct can be stored in RAM with the volatile keyword. So, always use PROGMEM keyword to store your variables into flash memory. If you do not use the volatile keyword, then it is up to the compiler where to store the variables either in RAM or register or totally discard them.

You are likely a victim of code optimization

try forcing it really in RAM

// // This struct in RAM
volatile Coeff C = {0.0048243,

When using the constants in ram, they are still present in flash as well, and the startup code copies the initialization code copies them over into RAM.

Definitely compiler optimization. Try this test code to see the difference:

#include <avr/pgmspace.h>

//comment out to test in ram
#define progmemtest

struct Coeff
{
  float b0;
  float b1;
  float b2;
  float b3;
  float b4;
  float a1;
  float a2;
  float a3;
  float a4;
};

#ifdef progmemtest
// This struct in flash
const Coeff C[] PROGMEM = {
  { 0.0048243,
    0.0192974,
    0.0289461,
    0.0192974,
    0.0048243,
    2.3695159,
    -2.3139877,
    1.0546647,
    -0.1873776
  },
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
};

#else
// This struct in RAM
const Coeff C[] = {
  { 0.0048243,
    0.0192974,
    0.0289461,
    0.0192974,
    0.0048243,
    2.3695159,
    -2.3139877,
    1.0546647,
    -0.1873776
  },
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
};
#endif

void setup()
{
  Serial.begin(9600);
  delay(1000);
#ifdef progmemtest
  // Read struct from progmem and print values
  Serial.println(pgm_read_float(&C[random(2)].b0), 8);
#else
  // Read struct from RAM and print values
  Serial.println(C[random(2)].b0, 8);
#endif
}

void loop()
{
  // put your main code here, to run repeatedly:

}

Which display and which library? Odd that increasing font size would cause the problem, since fonts are generally stored in PROGMEM. Does the display library work with the F() macro for text constants?

Yes, and again, an array goes into progmem, not a bare struct.

I’m using a Nokia display with the Adafruit libraries. 504 bytes out of the 328’s 2k. In an earlier version of the code, setting textsize to 3 caused the code to hang, cutting it back to 2 ran. That prompted me to put constants in flash. Of course all the text is in flash, and that works fine. Arrays go in ok, but this struct behaves oddly. The flash is clearly allocated, but the ram footprint does not decrease.