简体   繁体   English

有限内存平台上的数组指针的C ++数组(arduino)

[英]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数组:

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. 请注意,这是在arduino平台上运行的,它的内存非常有限。

I suspect that lookup into an array with identical elements is being optimized; 我怀疑正在优化查找具有相同元素的数组; If int const *text[]; 如果是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). 至少使用avr-libc(使用avr-gcc,avr-binutils),有宏或变量属性可以将这种常量数据放在更大的只读程序空间(闪存ROM)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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