简体   繁体   English

在C中的main函数中使用静态变量是否有意义?

[英]Does it make sense to use static variables in the main function in C?

As far as my current understanding of the 'static' keyword goes, it prevents a variable from being re-initialized AND it prevents the variable from leaving the memory when a function ends. 就我目前对'static'关键字的理解而言,它可以防止变量被重新初始化,并且它可以防止变量在函数结束时离开内存。

In C, I usually use this when a variable doesn't need to be global, but also shouldn't change in between function calls. 在C中,我通常在变量不需要是全局变量时使用它,但也不应该在函数调用之间进行更改。 (fi microcontroller interrupts) (fi微控制器中断)

Now, in some C code for an STM32, I saw the following: 现在,在STM32的一些C代码中,我看到了以下内容:

int main(void)
{
  static char buffer[CONSOLEBUFFERSIZE];
  ...

To me, this doesn't make sense. 对我来说,这没有意义。 This variable is used to buffer incoming commands in order to process them when the termination character is received. 此变量用于缓冲传入的命令,以便在收到终止字符时处理它们。 But the two properties of 'static' I described earlier do not apply to the main function because main() is called only once and 'never' ends. 但是我之前描述的'static'的两个属性并不适用于main函数,因为main()只调用一次而'never'结束。 So my actual question: 所以我的实际问题是:

Could this be using some hocus-pocus that I don't know about or would it simply be copied code from an interrupt or other function and did the programmers forget or not bother to remove the static keyword? 这可能是使用我不知道的一些hocus-pocus,还是仅仅是从中断或其他函数复制代码,程序员是否忘记了或者没有删除静态关键字?

One difference is, that static variables usually use the program's data segment instead of stack. 一个区别是,静态变量通常使用程序的数据段而不是堆栈。 Maybe that's the reason for declaring buffer as static (especially if CONSOLEBUFFERSIZE is large). 也许这就是将buffer声明为static的原因(特别是如果CONSOLEBUFFERSIZE很大)。

On some systems the stack is a fixed, limited size. 在某些系统上,堆栈是固定的,有限的大小。 In these cases static is useful simply to move the buffer out of the stack and put it somewhere where the linker has been set up to allocate more space. 在这些情况下, static仅用于将缓冲区移出堆栈并将其放置在已设置链接器的位置以分配更多空间。

It would also be possible to re-configure the linker to offer a larger initial stack, but static is easier and still does the right thing. 也可以重新配置链接器以提供更大的初始堆栈,但static更容易并且仍然是正确的。

我认为当你为他们制作包含许多文件和主管的交流项目时,它的价值不会改变....

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

相关问题 在 C 中,在函数中使用静态变量会使其更快吗? - In C, does using static variables in a function make it faster? 在extern C中使用_attribute __((nothrow))是否有意义? - Does it make sense to use _attribute__ ((nothrow)) inside extern C? 是否有正当的理由将变量声明为C程序的main()函数内部的静态变量? - Are there valid reasons to declare variables static inside the main() function of a C program? C main()函数可以是静态的吗? - Can the C main() function be static? 在C中多次声明一个函数有什么意义? - What sense does it make to declare a function more than one time in C? C中的静态变量(在main中声明)。 混合 - static variables in C (declared in main). a mixup 什么时候使用没有 typedef 的结构体有意义? - When does it make sense to use a struct without a typedef? 在标题中使用 ifdef linux 等是否有意义? - Does it make sense to use ifdef linux etc in header? 它是如何有意义的,为什么sscanf功能仍然有效? - How does it make sense and why is the sscanf function still working? c - 为什么将字符指针索引为int是有意义的? - c - why does it make sense that indexing a character pointer is an int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM