普通 C 中的静态变量和 extern

发布于 2024-09-01 04:48:11 字数 71 浏览 2 评论 0原文

在函数外部声明静态变量和在函数内部声明静态变量有区别吗?

另外,将变量声明为静态变量和仅声明外部变量有什么区别?

Is there a difference between declaring a static variable outside of a function and declaring a static variable inside a function?

Also, what's the difference between declaring a variable as static and just declaring an extern variable?

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

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

发布评论

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

评论(2

神仙妹妹 2024-09-08 04:48:11

不同之处在于,函数内部的静态变量仅在函数内部可见,而外部的静态变量可以被任何函数从其声明点到翻译单元末尾看到。

否则,他们的行为是一样的。

在函数外部声明一个变量而不使用关键字static意味着它在定义该变量的文件(翻译单元)外部(以及从定义点到该变量的末尾)是可见的(可访问的)。翻译单位)。如果将变量声明为 extern,则意味着在其他地方有定义 - 可能在同一个翻译单元中,但更有可能在另一个翻译单元中。请注意,可以在函数内部声明 extern 变量,但只能在函数外部定义它。

The difference is that the static variable inside the function is visible inside the function only, whereas the static variable outside may be seen by any function from its point of declaration to the end of the translation unit.

Otherwise, they behave the same.

Declaring a variable outside of a function without the keyword static means it is visible (accessible) outside the file (translation unit) where it is defined (as well as from the point of definition to the end of the translation unit). If you declare a variable as extern, it means that there is a definition somewhere else - possibly in the same translation unit, but more likely in another. Note that it is possible to declare an extern variable inside a function, but it can only be defined outside of a function.

同尘 2024-09-08 04:48:11

第一个例子:

class SoTest
{
public:
    SoTest(const char *name)
    {
        printf("C'tor called of %s\t(%#x) object\n",name,this);
    }
    static SoTest ClassStaticObj;
};

static SoTest CStaticObj("CStaticObj");
SoTest SoTest::ClassStaticObj("ClassStaticObj");

void function()
{
    for (int i = 0; i < 2;i++)
    {
        static SoTest FunctionStaticObj("FunctionStaticObj");
            SoTest FunctionObj("FunctionObj");
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("enter main\n");
    function();
    function();
    getchar();;
    return 0;
}

产生:

    C'tor called of CStaticObj      (0x419168) object
C'tor called of ClassStaticObj  (0x419169) object
enter main
C'tor called of FunctionStaticObj       (0x419160) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object

现在你的问题

有什么区别吗
在外部声明静态变量
一个函数并声明一个静态
函数内的变量?

这些是完全不同的问题
函数外的静态变量/函数在编译单元之外不可见。

函数内部的静态变量在全局数据中分配,并且在第一次出现时仅初始化一次(请参阅 FunctionStaticObj 在 main 之后初始化,这与其他对象相矛盾。

另外,有什么区别
将变量声明为静态变量并且
只是声明一个外部变量?

同样,静态意味着在编译单元外部不可见。

extern 的意思是“它没有在这里定义,尽管在不同的编译单元中,并且链接器会管理它”,因此您可以根据需要创建任意多个外部声明,但只能创建一个非外部定义。

First Example:

class SoTest
{
public:
    SoTest(const char *name)
    {
        printf("C'tor called of %s\t(%#x) object\n",name,this);
    }
    static SoTest ClassStaticObj;
};

static SoTest CStaticObj("CStaticObj");
SoTest SoTest::ClassStaticObj("ClassStaticObj");

void function()
{
    for (int i = 0; i < 2;i++)
    {
        static SoTest FunctionStaticObj("FunctionStaticObj");
            SoTest FunctionObj("FunctionObj");
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("enter main\n");
    function();
    function();
    getchar();;
    return 0;
}

produces:

    C'tor called of CStaticObj      (0x419168) object
C'tor called of ClassStaticObj  (0x419169) object
enter main
C'tor called of FunctionStaticObj       (0x419160) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object
C'tor called of FunctionObj     (0x12fe77) object

Now your question(s)

Is there a difference between
declaring a static variable outside of
a function and declaring a static
variable inside a function?

Those are completely different issues
static variable / function outside function is not visible outside compilation unit.

static variable inside function is allocated in global data and is initialized only once during first occurance (see that FunctionStaticObj was initialized after main in contradiction to other objects.

Also, what's the difference between
declaring a variable as static and
just declaring an extern variable?

Again static means not visible outside compilation unit.

extern means "it's not defined here, although in different compilation unit and linker will manage it" so you can make as many extern declarations as you want but only one non extern definition.

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