Pages: [1]   Go Down
Author Topic: PROGMEM question  (Read 400 times)
0 Members and 1 Guest are viewing this topic.
San Pedro, CA.
Offline Offline
Full Member
***
Karma: 3
Posts: 155
My head HURTS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know storing strings in flash memory saves on RAM, but does the same hold true for numeric constants?

For example:
Code:

// Say you have a function that takes four numbers

void foo(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
    /* do something here */
}

// Then for whatever reason it's used in a loop with data from an array

const uint8_t array[3][4] =
{
    {240, 54, 12, 53},
    {122, 8, 59, 22},
    {8, 0, 255, 127}
};

for (uint8_t i = 0; 3 > i; i++)
{
    foo(array[i][0], array[i][1], array[i][2], array[i][3]);
}

// This example is small, but let's say the array is much larger and with more loop iterations


Is there any benefit in putting the array in flash memory via PROGMEM, or is the array already stored there because it's a const data type?
Would it make a difference if it was not a const data type?

Thanks for any replies,
DJ
Logged

DigitalJohnson

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It all depends on the scenario its used in. For your example:

The compiler for arduino is set to optimisations favouring small code, so in this case it might not be compiled away due to it having to unroll the loop. If the loop was unrolled, then yes it could be a compile time constant, depending on its use elsewhere.

All of this depends on weather the variable is marked external, used by non-constant reference to any of the data, or something looks at the address of the array, and sometimes the compiler cannot guarantee a variable is a 'compile time constant', even when you clearly program it as one.

If you are going to expect/want the compiler to unroll the loop, then just do it in code yourself. Templates will help you achieve unrolling of complex loops ( not for the loop above ). And if you do unroll, you can then use an enum to organise the data, instead of an array, then you can guarantee the values are compile time constants.

Then you will also have to weigh the result of having unrolled code ( can get large ), vs an array in progmem used in a loop.
Logged


Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any benefit in putting the array in flash memory via PROGMEM, or is the array already stored there because it's a const data type?

There is a benefit of putting such arrays in PROGMEM RAM-wise but there is the drawback that you cannot access them the usual way. You have to read every byte using pgm_read_byte (or another member of that family of functions).
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any benefit in putting the array in flash memory via PROGMEM, or is the array already stored there because it's a const data type?

An array even if declared const is always stored in SRAM memory unless you use PROGMEM to force the array to be stored in flash memory.

Lefty
Logged

Pages: [1]   Go Up
Jump to: