gpt4 book ai didi

c++ - 关于静态局部变量的说明

转载 作者:搜寻专家 更新时间:2023-10-31 00:54:15 24 4
gpt4 key购买 nike

在链接中 Storage Class Specifier

在主题静态局部变量下我读过

Variables declared at block scope with the specifier static have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered).

我不明白说:

到底是什么意思

which can be performed before the block is first entered

另外,是否可以说static 局部变量具有No Linkage

如有必要,您能举一些例子吗?谢谢

最佳答案

相关部分是当初始值已知时。
当知道在 block 第一次执行之前它不会改变的额外保证。

下面是带有一些示例的代码。

int foo(void);

void bar(void)
{
static int ZeroInit; // zero init
static int a = 0; // constant init
static int b = 4; // constant init
// values for above inits are known at start of runtime and even earlier

static int c = foo();
// dynamic init, value unknown before execution of foo(),
// which cannot (i.e. is defined not to) be done before executing the block

/* code */
}

关于c++ - 关于静态局部变量的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46025570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com