简体   繁体   中英

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM