在嵌入式环境中,全局声明还是静态声明更安全?

发布于 2024-12-11 07:52:56 字数 252 浏览 0 评论 0原文

我可以选择将变量声明为静态变量还是全局变量。

我想在一个函数中使用变量来维护计数器。

例如

void count()
{
   static int a=0;
   for(i=0;i<7;i++)
   {
      a++;
   }

}

我的另一个选择是将变量 a 声明为全局变量。 我只会在这个函数 count() 中使用它。

哪种方式是最安全的解决方案?

I have a choice to between declaring a variable static or global.

I want to use the variable in one function to maintain counter.

for example

void count()
{
   static int a=0;
   for(i=0;i<7;i++)
   {
      a++;
   }

}

My other choice is to declare the variable a as global.
I will only use it in this function count().

Which way is the safest solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

若相惜即相离 2024-12-18 07:52:56

它仅在编译和链接时重要。 static 局部变量的存储和初始化方式应与全局变量完全相同。

声明本地静态变量仅影响其在语言级别的可见性,使其仅在封闭函数中可见,尽管具有全局生命周期。

未标记为static 的全局变量(或一般的任何对象)具有外部链接,链接器在合并每个对象文件时将考虑该符号。

标记为static的全局变量仅在当前翻译单元内具有内部链接,并且链接器在合并各个翻译单元时不会看到这样的符号。

It matters only at compile and link-time. A static local variable should be stored and initialised in exactly the same way as a global one.

Declaring a local static variable only affects its visibility at the language level, making it visible only in the enclosing function, though with a global lifetime.

A global variable (or any object in general) not marked static has external linkage and the linker will consider the symbol when merging each of the object files.

A global variable marked static only has internal linkage within the current translation unit, and the linker will not see such a symbol when merging the individual translation units.

素食主义者 2024-12-18 07:52:56

从代码可读性的角度来看,内部静态可能更好,如果您只在该函数内部使用它。

如果它是全局的,其他一些函数可能会修改它,这可能是危险的。

The internal static is probably better from a code-readability point of view, if you'll only ever use it inside that function.

If it was global, some other function could potentially modify it, which could be dangerous.

笑看君怀她人 2024-12-18 07:52:56

在函数中使用全局变量或静态变量都不安全,因为这样您的函数将不再是可重入

但是,如果您不关心函数可重入,那么您可以根据您的选择选择其中之一。

Either using global or static variable within a function both are not safe because then your function will no longer be re-entrant.

However if you are not concerned with function being re-entrant then you can have either based on your choice.

万水千山粽是情ミ 2024-12-18 07:52:56

如果该变量只能在函数 count() 中访问,那么根据定义它是本地的,所以我不明白为什么会出现这个问题。作为一项规则,始终对任何符号使用尽可能严格的范围。

你真的应该阅读 Jack Ganssle 的文章 全局变量,这将是有启发性的。

If the variable is only to be accessed within the function count() then it is by definition local, so I cannot see why the question arises. As a rule, always use the most restrictive scope possible for any symbol.

You should really read Jack Ganssle's article A Pox on Globals, it will be enlightening.

留蓝 2024-12-18 07:52:56

始终尽可能缩小范围。如果变量不需要在函数外部可见,则也不应该在函数外部声明它。应尽可能使用 static 关键字。如果在文件范围内声明变量,则它应该始终是静态的,以将范围缩小到声明它的文件。这是 C 的私有封装方式。

上述内容适用于所有系统。对于嵌入式,还有另一个问题:所有声明为静态全局的变量都必须在程序启动之前初始化。这是由 ISO C 强制执行的。因此它们始终设置为程序员希望它们初始化的值。如果程序员没有设置任何值,它们将被初始化为零(或 NULL)。

这意味着在调用 main 之前,程序中必须执行一个片段来设置所有这些静态/全局值。在嵌入式系统中,初始化值从 ROM(闪存、eeprom 等)复制到 RAM。标准 C 编译器通过创建此代码片段并将其添加到程序中来处理此问题。

然而,在嵌入式系统中,这个片段通常是不幸的,因为它会导致程序启动时的延迟,特别是在有大量静态/全局的情况下。大多数嵌入式编译器支持的常见非标准优化是删除此代码片段。该程序将不再按照 C 标准的预期运行,但速度会更快。完成此优化后,必须在运行时完成初始化,大致为static int x; x=0; 而不是 static int x=0;

为了使您的程序可移植到此类非标准嵌入式编译器,始终在运行时设置全局/静态是一个好习惯。无论您是否打算移植到此类编译器,不依赖全局/静态的默认零初始化当然是一个好习惯。因为大多数菜鸟 C 程序员甚至不知道这个静态零初始化规则的存在,如果您在使用变量之前不显式初始化它们,他们会感到非常困惑。

Always reduce scope as far as possible. If a variable doesn't need to be visible outside a function, it should not be declared outside it either. The static keyword should be used whenever possible. If you declare a variable at file scope, it should always be static to reduce the scope to the file it was declared in. This is C's way of private encapsulation.

The above is true for all systems. For embedded there is another concern: all variables declared as static or global must be initialized before the program is started. This is enforced by ISO C. So they are always set either to the value the programmer wants them initialized to. If the programmer didn't set any value they are initialized to zero (or NULL).

This means that before main is called, there must be a snippet executed in your program that sets all these static/global values. In an embedded system, the initialization values are copied from ROM (flash, eeprom etc) to RAM. A standard C compiler handles this by creating this snippet and adding it to your program.

However, in embedded systems this snippet is often unfortunate, as it leads to a delay at program startup, especially if there is lots of statics/globals. A common non-standard optimization most embedded compilers support, is to remove this snippet. The program will then no longer behave as expected by the C standard, but it will be faster. Once you have done this optimization, initialization must be done in runtime, roughly static int x; x=0; rather than static int x=0;.

To make your program portable to such non-standard embedded compilers, it is a good habit to always set your globals/statics in runtime. And no matter if you intend to port to such compilers or not, it is certainly a good habit not to rely on the default zero initialization of globals/statics. Because most rookie C programmers don't even know that this static zero initialization rule exists and they will get very confused if you don't init your variables explicitly before using them.

唐婉 2024-12-18 07:52:56

我不认为静态和静态有什么特别之处。带有嵌入域的正常全局 ...!!

从某种意义上说,静态是好的,如果你打算在开始时将计数器初始化为 o,那么如果你只是用 static 声明,那么就不需要用它初始化 0,因为每个静态变量默认都用 0 初始化。

编辑:
在克利福德的评论之后,我检查并了解到全局变量也是静态分配并初始化为零,因此这种优势不存在。

i dont think is there is anything special with static & normal global with embedded domain ...!!

in one way static is good that if you are going to initialize your counter as o in starting then if you just declare with static then there is no need to initialize with it 0 because every static varaible is by default initialized with 0.

Edit :
After Clifford's comment i have checked and get to know that globals are also statically allocated and initialised to zero, so that advantage does not exist..

狂之美人 2024-12-18 07:52:56

而是传递指向“标准”变量的指针

void count(int *a) {
    int i;
    for (i = 0; i < 7; i++)
    {
        (*a)++;
    }
}

这样您既不依赖全局变量也不依赖静态局部变量,这使您的程序更好。

Pass a pointer to a "standard" variable instead

void count(int *a) {
    int i;
    for (i = 0; i < 7; i++)
    {
        (*a)++;
    }
}

This way you do not rely neither on global variables nor on static local variables, which makes your program better.

長街聽風 2024-12-18 07:52:56

我想说,如果您只想一个函数来访问您声明它的函数,那么静态比全局更好。另外,全局变量更容易被其他函数意外访问。

如果您确实想要使用全局变量,因为它可以被程序中的其他函数访问,请确保将它们声明为易失性

volatile int a = 0;

易失性确保编译器不会以错误的方式对其进行优化。

I would say static is better than global if you want only one function to access it in which you declared it . Plus global variables are more prone to be accidentally accessed by other functions.

If you do want to use globals since it can be accessed by other functions in the program, make sure you declare them as volatile .

volatile int a = 0;

volatile makes sure it is not optimised by compilers in the wrong way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文