在实时嵌入式设备的实现中,每个 channel 都有一个状态寄存器变量(让我们盲目地假设我的嵌入式设备有多个 channel ,并且必须为每个 channel 完成一些工作)。
下面是状态变量当前的声明方式:
struct channel_status status[NCHANNELS];
性能方面,最好使用一个中间全局变量,该变量是所选 channel 的 status
变量的副本。
例子:
struct channel_status status_shadow;
void some_work() {
for(channel = 0; channel < NCHANNELS; channel++) {
status_shadow = status[channel];
foo(); // Function that use the status_shadow as global
bar(); // "
baz(); // "
status[channel] = status_shadow;
}
我不是在讨论实现,也不是在讨论使用指针而不是变量的可能性。我的问题与中间变量的名称有关。
我选择了 status_shadow
因为我觉得我正在做某种 shadowing .
Is there a better/more accurate technical name for such intermediate variable ?
实现注意事项:
我决定使用这个中间变量的原因是因为将 channel 指针i
或状态变量传递给每个函数foo
太耗费资源了, bar
, baz
, ... 在性能方面,避免堆栈 push/pop 可以在实时应用程序中节省一些宝贵的时间。
最佳答案
从技术上讲,您不是在跟随;您必须定义一个具有相同名称 的变量来隐藏它。此外,阴影通常不受欢迎,因为粗心的使用可能导致容易混淆。
您正在做的是为您的周期获取当前项目,因此合适的名称可以是current_status
或cur_status
。如果您将它用作参数,那么名称将只包含在 for()
中,它可能是 current
或 cur_item
作为好吧。
另一个想法可能是 temp_channel_status
,这意味着尽管该变量是全局变量,但该值不应被视为固定值。
关于c - 正确的变量名是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30478936/