C/C++全局与静态全局

发布于 2024-12-11 04:15:52 字数 249 浏览 0 评论 0原文

可能的重复:
静态与全局

我对全局变量和静态全局变量之间的差异感到困惑。如果静态意味着该变量仅对于同一文件是全局的,那么为什么在两个不同的文件中相同的名称会导致名称冲突?

有人可以解释一下吗?

Possible Duplicate:
Static vs global

I'm confused about the differences between global and static global variables. If static means that this variable is global only for the same file then why in two different files same name cause a name collisions?

Can someone explain this?

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

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

发布评论

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

评论(2

仅此而已 2024-12-18 04:15:53

当您创建可供链接器在其他文件中使用的 .o 文件时,全局变量(不是static)就在那里。因此,如果您有两个这样的文件,则会在 a 上发生名称冲突:

ac:

#include <stdio.h>

int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

bc:

int a;

int compute(void)
{
    a = 0;
    return a;
}

因为链接器不知道要使用哪个全局 a

但是,当您定义静态全局变量时,您是在告诉编译器仅为该文件保留变量,并且不要让链接器知道它。因此,如果您将 static (在 a 的定义中)添加到我编写的两个示例代码中,您将不会发生名称冲突,因为链接器甚至不知道这两个文件中都有一个 a

ac:

#include <stdio.h>

static int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

bc:

static int a;

int compute(void)
{
    a = 0;
    return a;
}

这意味着每个文件都使用自己的 a,而不知道其他文件。


附带说明一下,只要它们位于不同的文件中,就可以将其中一个设为静态,而另一个则不然。如果两个声明位于同一文件中(读取翻译单元),一个 static 和一个 extern,请参阅 这个答案

Global variables (not static) are there when you create the .o file available to the linker for use in other files. Therefore, if you have two files like this, you get name collision on a:

a.c:

#include <stdio.h>

int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

int a;

int compute(void)
{
    a = 0;
    return a;
}

because the linker doesn't know which of the global as to use.

However, when you define static globals, you are telling the compiler to keep the variable only for that file and don't let the linker know about it. So if you add static (in the definition of a) to the two sample codes I wrote, you won't get name collisions simply because the linker doesn't even know there is an a in either of the files:

a.c:

#include <stdio.h>

static int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

static int a;

int compute(void)
{
    a = 0;
    return a;
}

This means that each file works with its own a without knowing about the other ones.


As a side note, it's ok to have one of them static and the other not as long as they are in different files. If two declarations are in the same file (read translation unit), one static and one extern, see this answer.

对风讲故事 2024-12-18 04:15:53

每个文件中的静态名称不应导致名称冲突。如果您看到了这一点,请发布(简短的)演示代码来显示它,以及您正在使用的确切编译器,以便我们可以正确验证代码并假设它是正确的,适当地诽谤编译器。

FWIW,C++ 中的首选方法是使用匿名名称空间:

namespace { 
    int not_a_static_variable;
}

老实说,不,我不能指出这样做有很多客观优势......

A name that's static in each file should not cause name collisions. If you're seeing that, please post (short) demo code showing it, along with the exact compiler you're using so we can properly verify the code and assuming it's correct, proper vilify the compiler.

Just FWIW, the preferred method in C++ is to use an anonymous namespace instead:

namespace { 
    int not_a_static_variable;
}

In all honesty, no I can't point to a lot of objective advantage to that though...

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