# Why 'static const ... PROGMEM' for ActiveLayerColor

**URL:** https://community.keyboard.io/t/why-static-const-progmem-for-activelayercolor/3117
**Category:** Programming
**Created:** [August 15, 2019, 11:22pm UTC](https://community.keyboard.io/t/why-static-const-progmem-for-activelayercolor/3117 "2019-08-15T23:22:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MaxG](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MaxG](https://community.keyboard.io/u/MaxG)
#### Post date: [August 15, 2019, 11:22pm UTC](https://community.keyboard.io/t/why-static-const-progmem-for-activelayercolor/3117/1 "2019-08-15T23:22:31Z")

</div>

Hi all,

the example source code for ActiveLayerColor (and probably some other plugins too) has a variable definition of

```
static const cRGB foo[] PROGMEM = …

```

I tried to turn it to plain

```
constexpr cRGB foo[] = …

```

and thought that’s safe due to constexpr forcing everything to be calculatable at compile time.

However, I got weird behaviour and switch back to the code from the example layout. Know I wonder two things:

- What is PROGMEM and how does it work? It does not seem to be valid C++ for me.
- Why `static`?
- Why didn’t `constexpr` safe me?

I hope that the answer to this questions would be very educative for others too. Could anyone point me out to the documentation please? I could not find the answers myself.

Thank you very much.

---

<div class="post-metadata">

### Author: ![algernon](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/algernon/32/4404_2.png) [@algernon](https://community.keyboard.io/u/algernon)
#### Post date: [August 16, 2019, 12:04am UTC](https://community.keyboard.io/t/why-static-const-progmem-for-activelayercolor/3117/2 "2019-08-16T00:04:46Z")

</div>

`PROGMEM` is a magic preprocessor macro that tells the compiler to place the data into flash memory instead of SRAM. The AVR architecture used by the ATMega32u4 MCU (the heart of the keyboard) has two kinds of memory: flash memory and SRAM. Flash memory is read-only, except when flashing the keyboard. SRAM is read-write. The reason we put a lot of things into PROGMEM (short for “program memory”) is because there’s considerably more of that on the MCU: there’s about 28k flash, and only 2k SRAM. So anything that’s read-only, is usually put into flash, to save SRAM for things that do need to be writeable.

The relevant arduino docs are here:

[https://www.arduino.cc/reference/en/language/variables/utilities/progmem/](https://www.arduino.cc/reference/en/language/variables/utilities/progmem/)

`constexpr` didn’t save you, because even though that calculates things at compile time, the result will still be put in SRAM, while the code expects it to be in PROGMEM/flash.

---

<div class="post-metadata">

### Author: ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)
#### Post date: [August 16, 2019, 1:33am UTC](https://community.keyboard.io/t/why-static-const-progmem-for-activelayercolor/3117/3 "2019-08-16T01:33:38Z")

</div>

> [@MaxG](#):
>
> Why `static` ?

In this case, the array is defined inside the `setup()` function, then a pointer to that array is set which gets used after `setup()` has returned. If it wasn’t declared `static`, the memory allocated for that array could be freed and then overwritten, resulting in unpredictable behaviour when the pointer is used.

In this case, however, the memory can’t be overwritten, because it’s read-only program memory, as @algernon already explained. If the compiler doesn’t detect an error, it would probably work just fine without declaring it `static`, but it would be poor practice to do so.
