gpt4 book ai didi

c - 使用指定的初始化程序初始化数组时出现奇怪的值

转载 作者:太空狗 更新时间:2023-10-29 16:29:21 25 4
gpt4 key购买 nike

当我初始化下面的数组时,除了 values[3] 之外,所有输出看起来都正常。出于某种原因,初始化为 values[0]+values[5]values[3] 正在输出一个非常大的数字。我的猜测是我试图在它们正确存储在内存中之前分配 values[0]+values[5] 但如果有人能解释那会很棒。

int main (void)
{

int values[10] = {
[0]=197,[2]=-100,[5]=350,
[3]=values[0] + values[5],
[9]= values[5]/10
};

int index;

for (index=0; index<10; index++)
printf("values[%i] = %i\n", index, values[index]);


return 0;
}

输出如下:

values[0] = 197
values[1] = 0
values[2] = -100
values[3] = -1217411959
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

最佳答案

看起来你在这里受到未指定的行为的影响,因为初始化列表表达式的评估顺序是未指定的,来自草案 C99 标准部分 6.7.8:

The order in which any side effects occur among the initialization list expressions is unspecified.133)

注释 133 说:

In particular, the evaluation order need not be the same as the order of subobject initialization.

据我所知,支持注释 133 的规范文本来自 6.5 部分:

s>

Except as specified later [...] the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

我们可以看到 intializer 是来自 6.8 的完整表达式(强调我的):

A full expression is an expression that is not part of another expression or of a declarator. Each of the following is a full expression: an initializer; [...]

回顾我的一个 old C++ answers它涵盖了初始化程序中的序列点,并将 完整表达式 放在一个不同的地方然后我最初得出结论,我意识到 6.7.8 中的语法包含 initializer 两次:

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
initializer-list:
designationopt initializer
initializer-list , designationopt initializer

我最初没有注意到这一点,并认为完整表达式 的声明适用于上述语法中的顶部元素。

我现在相信像 C++ 一样,完整表达式 适用于 initializer-list 中的每个initializer,这使我之前的分析不正确。

Defect report 439证实了我的怀疑,确实如此,它包含以下示例:

#include <stdio.h>

#define ONE_INIT '0' + i++ % 3
#define INITIALIZERS [2] = ONE_INIT, [1] = ONE_INIT, [0] = ONE_INIT

int main()
{
int i = 0;
char x[4] = { INITIALIZERS }; // case 1
puts(x);
puts((char [4]){ INITIALIZERS }); // case 2
puts((char [4]){ INITIALIZERS } + i % 2); // case 3
}

它说:

In every use of the INITIALIZERS macro, the variable i is incremented three times. In cases 1 and 2, there is no undefined behavior, because the increments are in expressions that are indeterminately sequenced with respect to one another, not unsequenced.

因此 INITIALIZERS 中的每个初始化器 都是一个完整表达式

由于此缺陷报告是针对 C11 的,因此值得注意的是,在有关此问题的规范文本中,C11 比 C99 更冗长,它说:

The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified.152)

在将 values 中的各个元素分配给之前评估以下表达式的情况下存在未定义的行为:

 values[0] + values[5]

或:

 values[5]/10

这是未定义的行为,因为使用了 indeterminate value invokes undefined behavior .

在这种特定情况下,最简单的解决方法是手动执行计算:

int values[10] = { 
[0]=197,[2]=-100,[5]=350,
[3]= 197 + 350,
[9]= 350/10
};

还有其他替代方案,例如在初始化后对元素 39 进行赋值。

关于c - 使用指定的初始化程序初始化数组时出现奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813617/

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