C 中的内部静态变量,你会使用它们吗? [英] Internal static variables in C, would you use them?

查看:15
本文介绍了C 中的内部静态变量,你会使用它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,您可以拥有外部静态变量,这些变量在文件中的每个位置都可见,而内部静态变量仅在函数中可见,但具有持久性

In C you can have external static variables that are viewable every where in the file, while internal static variables are only visible in the function but is persistent

例如:

#include <stdio.h>

void foo_bar( void )
{
        static counter = 0;
        printf("counter is %d
", counter);
        counter++;
}
int main( void )
{
        foo_bar();
        foo_bar();
        foo_bar();
 return 0;
}

输出将是

counter is 0
counter is 1
counter is 2

我的问题是为什么要使用内部静态变量?如果您不希望您的静态变量在文件的其余部分可见,那么该函数不应该在它自己的文件中吗?

My question is why would you use an internal static variable? If you don't want your static variable visible in the rest of the file shouldn't the function really be in its own file then?

推荐答案

这种混淆通常是因为 static 关键字有两个目的.

This confusion usually comes about because the static keyword serves two purposes.

当在文件级别使用时,它控制编译单元之外的对象的可见性,而不是对象的持续时间(可见性和持续时间是我使用的外行术语在教育课程中,ISO 标准使用了不同的术语,您最终可能想要学习这些术语,但我发现它们让大多数刚入门的学生感到困惑).

When used at file level, it controls the visibility of its object outside the compilation unit, not the duration of the object (visibility and duration are layman's terms I use during educational sessions, the ISO standard uses different terms which you may want to learn eventually, but I've found they confuse most beginning students).

在文件级别创建的对象的持续时间已经确定,因为它们处于文件级别.static 关键字只是让它们对链接器不可见.

Objects created at file level already have their duration decided by virtue of the fact that they're at file level. The static keyword then just makes them invisible to the linker.

在函数内部使用时,它控制持续时间,而不是可见性.可见性已经确定,因为它在函数内部——它不能在函数外部被看到.在这种情况下,static 关键字导致对象与文件级对象同时创建.

When used inside functions, it controls duration, not visibility. Visibility is already decided since it's inside the function - it can't be seen outside the function. The static keyword in this case, causes the object to be created at the same time as file level objects.

请注意,从技术上讲,在首次调用该函数之前,函数级静态可能不一定存在(这对于带有构造函数的 C++ 来说可能是有意义的),但我曾经使用过的每个 C 实现都会创建其函数级静态同时作为文件级对象.

Note that, technically, a function level static may not necessarily come into existence until the function is first called (and that may make sense for C++ with its constructors) but every C implementation I've ever used creates its function level statics at the same time as file level objects.

另外,虽然我使用对象"这个词,但我并不是指 C++ 对象的意思(因为这是一个 C 问题).这只是因为 static 可以应用于文件级别的变量或函数,我需要一个包罗万象的词来描述这一点.

Also, whilst I'm using the word "object", I don't mean it in the sense of C++ objects (since this is a C question). It's just because static can apply to variables or functions at file level and I need an all-encompassing word to describe that.

函数级静态仍然被大量使用——如果不满足,它们可能会在多线程程序中引起麻烦,但是,如果你知道你在做什么(或者你没有线程),它们就是在多个函数调用中保持状态同时仍提供封装的最佳方法.

Function level statics are still used quite a bit - they can cause trouble in multi-threaded programs if that's not catered for but, provided you know what you're doing (or you're not threading), they're the best way to preserve state across multiple function calls while still providing for encapsulation.

即使使用线程,您也可以在函数中使用一些技巧(例如在函数内分配线程特定的数据)使其可以工作,而不会不必要地暴露函数内部.

Even with threading, there are tricks you can do in the function (such as allocation of thread specific data within the function) to make it workable without exposing the function internals unnecessarily.

我能想到的唯一其他选择是全局变量和每次向函数传递一个状态变量".

The only other choices I can think of are global variables and passing a "state variable" to the function each time.

在这两种情况下,您都将函数的内部工作暴露给其客户端,并使函数依赖于客户端的良好行为(总是一个冒险的假设).

In both these cases, you expose the inner workings of the function to its clients and make the function dependent on the good behavior of the client (always a risky assumption).

这篇关于C 中的内部静态变量,你会使用它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆