I have a big array uint8_t [95][5]
containing the 7x5 bitmaps various characters (1 bit in each byte is unused). I am only using a few of these 95 characters, but the full static array is stored in program memory anyway.
I am working with an AVR, which has limited program memory, hence every byte counts! So I tried to rewrite the array as a number of template specialized classes (see below), but it did not solve the problem. When I import the header file containing all definitions, all characters become part of my executable, regardless if they are used or not in the program.
For exmaple, the following code
#include <stdint.h>
template <char C> struct BitMap { static uint8_t cols[5]; };
template <> uint8_t BitMap<'3'>::cols[] = {0xFF,0xFC,0xAB};
int main() { return 0; }
compiled with g++ -Os
generates
main:
xor eax, eax
ret
BitMap<(char)51>::cols:
.byte -1
.byte -4
.byte -85
.zero 2
Since this static array is not used in the program, is there a way to instruct the compiler/linker to get rid of it? Alternatively, is there any code trick I can do so that the array is instantiated only if I use it?