我不知道为什么会这样。根据我的理解,只要启用了优化,就不应该使用更多的RAM。它可能会使用更多的ROM(如果你使用变量),因为任何访问必须直接进入RAM(因为易失性),并且不能通过使用寄存器来优化,这可能需要更多的操作。这与波动无关。易挥发的手段,每个访问(读或写)需要应用于变量。理论上,它可以通过其他效果(例如,外部输入,或中断或通过任何方式)随时改变。编译器不允许将变量保存在寄存器中,以优化和重用不同操作的值。Stutt的意思是,你正在创建你想要的任何类型的多个变量的“结构”,甚至可以包括数组、联合或其他结构。
以上来自于百度翻译
以下为原文
I don't know why this is the case. To my understanding it should not use more RAM, as long as you have optimization enabled. It might use more ROM (if you use the variable), because any access must go to RAM directly (because of volatile) and cannot be optimized by using registers, which might require more operations.
This has nothing to do with volatile. Volatile means, that every access (read or write) needs to get applied to the variable. In theory it could get changed anytime by other effects (for example by an external input, or by an interrupt or by whatever). The compiler is not allowed to keep the variable in a register for optimization and reuse that value for different operations.
What you are describing is only because of the struct. Struct means, you are creating a "structure" of multiple variables of any type you want, which can even include arrays, unions or other structs.