c++ - Arduino 中的预定义数组

标签 c++ arrays arduino

我遇到了一个非常奇怪的问题,无论数组的大小如何,程序使用的内存量似乎都没有改变。

我正在使用 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/

相关文章:

arduino - 在 AVR Studio 中使用自动完成功能通过 avr-gcc 编码 C

c++ - 在构造函数中初始化私有(private)成员

c++ - 在 avr g++ 中禁用函数声明错误

c++ - 如何将此结构传递给可用函数?

c++ - 某些函数调用未显示在 VTune 中

c++ - 简单 Bison 脚本的段错误

javascript - 从 JavaScript 文件名数组创建变量

c++ - 如何柯里化(Currying)对象上的方法以将其作为 C 风格回调传递?

ios - 重置保存的 map 图钉,iOS Swift

php - 使用数组批量插入表