gpt4 book ai didi

c - arr[0] = arr[1] = C 中的值是不好的做法吗?

转载 作者:行者123 更新时间:2023-12-04 00:01:05 24 4
gpt4 key购买 nike

正如我上面所说,这是不好的做法吗?在 ASM 中它会是什么样子?我的意思是,我不知道它是否被翻译成这样:

arr[0] = value;
arr[1] = value;

或者是这样的:

arr[1] = value;
arr[0] = arr[1];

第二个显然效率更低(imm vs mem)。

提前致谢。

最佳答案

根据 C 语法赋值运算符从右到左求值。

所以这个说法

arr[0] = arr[1] = value;

等价于

arr[0] = ( arr[1] = value );

来自 C 标准(6.5.16 赋值运算符)

3 An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,111) but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.

所以根据报价你可以考虑这个说法

arr[0] = arr[1] = value;

喜欢

arr[1] = value;
arr[0] = arr[1];

至于效率,编译器可以生成与您想象的源代码不同的高效目标代码。

关于c - arr[0] = arr[1] = C 中的值是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61599445/

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