我遇到了一个非常奇怪的问题,无论数组的大小如何,程序使用的内存量似乎都没有改变。
我正在使用 Arduino 编译器,代码示例如下:
unsigned char ary[]={0x00,0x01,0x02,0x03,0x04,0x00,0x01,0x02,0x03,0x00,
0x01,0x02,0x03,0x04,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,};
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
如果我删除数组并重新编译,文件大小仍然相同。也许我已经离开编码领域有一段时间了,对可能发生的事情有什么想法吗?
unsigned char ary[] = {
0x00,0x01,0x02,0x03,0x04,0x00,0x01,0x02,0x03,0x00,
0x01,0x02,0x03,0x04,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,
};
unsigned char var1;
void setup() {
// put your setup code here, to run once:
int res = myFunction(&ary[0]);
}
void loop() {
// put your main code here, to run repeatedly:
}
int myFunction (unsigned char *adr){
//unsigned char b=4;
unsigned char c;
for(int n=0;n<1;n++)c=adr[0];
var1=c;
return 0;
}
最佳答案
Arduino 编译器自动取出未在任何地方使用的代码部分:死代码消除。
例如,如果您添加
ary[0] = 5;
在 setup 或 loop 部分,sketch 尺寸会增加,并且会在向 ary
添加元素时继续增加。
没有添加的行,我有一个 444 字节的草图(和 9 字节的动态内存)。添加该行后,它是 494 字节(和 31 字节的内存)。
关于c++ - Arduino 中的预定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48721587/