函数内部静态变量的使用

发布于 2024-10-08 08:45:48 字数 263 浏览 3 评论 0原文

我已经编写 C 代码很多年了,但最近我遇到了一个我从未使用过的功能:函数内的静态变量。因此,我想知道您使用此功能的方式有哪些,这是正确的设计决策。

例如,

int count(){
    static int n;
    n = n + 1;
    return n;
}

这是一个糟糕的设计决策。为什么?因为稍后您可能想要减少计数,这将涉及更改函数参数,更改所有调用代码,...

希望这足够清楚, 谢谢!

I have been writing C code for many years, but I recently came accross a feature that I have never used: a static variable inside a function. Therefore, I was wondering what are some ways that you have used this feature and it was the right design decision.

E.g.

int count(){
    static int n;
    n = n + 1;
    return n;
}

is a BAD design decision. why? because later you might want to decrement the count which would involve changing the function parameters, changing all calling code, ...

Hopefully this is clear enough,
thanks!

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

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

发布评论

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

评论(4

残花月 2024-10-15 08:45:48
void first_call()
{
   static int n = 0;

   if(!n) {
     /* do stuff here only on first call */
     n++;
   }

   /* other stuff here */
}
void first_call()
{
   static int n = 0;

   if(!n) {
     /* do stuff here only on first call */
     n++;
   }

   /* other stuff here */
}
似最初 2024-10-15 08:45:48

我在测试代码中使用静态变量来延迟初始化状态。在生产代码中使用静态局部变量充满危险,并可能导致微妙的错误。似乎(至少在我通常处理的代码中)几乎任何以单线程代码块开始的代码都有一个令人讨厌的习惯,即最终在并发情况下工作。在并发环境中使用静态变量可能会导致难以调试的问题。这样做的原因是因为由此产生的状态变化本质上是一个隐藏的副作用。

I have used static variables in test code for lazy initialization of state. Using static local variables in production code is fraught with peril and can lead to subtle bugs. It seems (at least in the code that I generally work on) that nearly any bit of code that starts out as a single-threaded only chunk of code has a nasty habit of eventually ending up working in a concurrent situation. And using a static variable in a concurrent environment can result in difficult issues to debug. The reason for this is because the resulting state change is essentially a hidden side effect.

老旧海报 2024-10-15 08:45:48

我使用静态变量作为控制另一个线程执行的方法。

例如,线程#1(主线程)首先声明并初始化一个控制变量,例如:

/* on thread #1 */
static bool run_thread = true;
// then initialize the worker thread

然后它开始执行线程#2,该线程将执行一些工作,直到线程#1决定停止它:

/* thread #2 */
while (run_thread)
{
  // work until thread #1 stops me
}

I have used static variables as a way to control the execution of another thread.

For instance, thread #1 (the main thread) first declares and initializes a control variable such as:

/* on thread #1 */
static bool run_thread = true;
// then initialize the worker thread

and then it starts the execution of thread #2, which is going to do some work until thread #1 decides to stop it:

/* thread #2 */
while (run_thread)
{
  // work until thread #1 stops me
}
笑脸一如从前 2024-10-15 08:45:48

有一个突出的例子,您非常需要保持静态来保护关键部分,即互斥锁。作为 POSIX 线程的示例:

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mut);
/* critical code comes here */
pthread_mutex_unlock(&mut);

这不适用于 auto 变量。

POSIX 有用于互斥体、条件和一次变量的静态初始值设定项。

There is one prominent example that you very much need to be static for protecting critical sections, namely a mutex. As an example for POSIX threads:

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mut);
/* critical code comes here */
pthread_mutex_unlock(&mut);

This wouldn't work with an auto variable.

POSIX has such static initialzers for mutexes, conditions and once variables.

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