Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ array of pointers to array on limited memory platform (arduino)

for each letter in the alphabet i have an int-array declared like this:

int const  A[64] ={ 
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,1,1,1,0,0,0,0,
    0,1,0,1,0,0,0,0,
    0,1,1,1,0,0,0,0,
    0,1,0,1,0,0,0,0,
    0,1,0,1,0,0,0,0,
    0,0,0,0,0,0,0,0
};

then i create another array with pointers to these.

int const * text[] = { A, B, C };

this works fine, until that text array reaches a certain number of different entries.

for example this works:

int const * text[] = { A, A, A, A, A, A, A, A }; // could even go on much longer

but this crashes:

int const * text[] = { A, B, C, D }; // it seems the number of different entries matters

why is that? i thought that if it is pointers, then it should not matter what it points to it will always be of constant size?

note that this is run on the arduino platform, which has very limited memory.

like image 905
clamp Avatar asked Feb 26 '12 14:02

clamp


1 Answers

I suspect that lookup into an array with identical elements is being optimized; If int const *text[]; were declared in a header file and compiled (defined) in a separate object file, you would likely see the same problem. The linker is doing the best it can, but all that data is likely overlapping with the heap / stack space.

At least with avr-libc (using avr-gcc, avr-binutils), there are macros, or variable attributes, that can place this sort of constant data in the much larger, read-only program space (flash ROM).

like image 155
Brett Hale Avatar answered Oct 15 '22 13:10

Brett Hale