admin管理员组文章数量:1432205
demo.c
#include<stdio.h>
int main(){
static int ia[2];
static volatile _Atomic (int *) a = (int *) (&ia[1]);
if ((a += (1)) != (int *) ((int *) (&ia[1]) + (1)))
printf("1");
}
compile failed
<source>:5:9: error: invalid operands to binary expression ('volatile _Atomic(int *)' and 'int')
5 | if ((a += (1)) != (int *) ((int *) (&ia[1]) + (1)))
| ~ ^ ~~~
1 error generated.
Compiler returned: 1
Gcc is ok!
Does clang support _Atomic Pointer Arithmetic
demo.c
#include<stdio.h>
int main(){
static int ia[2];
static volatile _Atomic (int *) a = (int *) (&ia[1]);
if ((a += (1)) != (int *) ((int *) (&ia[1]) + (1)))
printf("1");
}
compile failed
<source>:5:9: error: invalid operands to binary expression ('volatile _Atomic(int *)' and 'int')
5 | if ((a += (1)) != (int *) ((int *) (&ia[1]) + (1)))
| ~ ^ ~~~
1 error generated.
Compiler returned: 1
Gcc is ok!
https://godbolt./z/fMqzoe4b5
Does clang support _Atomic Pointer Arithmetic
Share Improve this question edited Nov 19, 2024 at 6:57 Peter Cordes 368k49 gold badges717 silver badges982 bronze badges asked Nov 19, 2024 at 3:25 zhangtianhaozhangtianhao 333 bronze badges 3 |1 Answer
Reset to default 4This is legal C code. The fact that clang rejects it is a bug, reported in 2015 and still not fixed.
As a workaround, you can use ++a
in this special case where you are adding 1, or for more general addition, you can use atomic_fetch_add
which does work on pointers. Note however that atomic_fetch_add
returns the old value rather than the new value, so to get the equivalent of a += 5
, you'd have to write something like
atomic_fetch_add(&a, 5)+5
.
本文标签: cDoes clang support Atomic Pointer ArithmeticStack Overflow
版权声明:本文标题:c - Does clang support _Atomic Pointer Arithmetic - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584194a2664795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
a++
succeeds, as doesatomic_fetch_add(&a, n)
. – Nate Eldredge Commented Nov 19, 2024 at 4:47a += n
to work for_Atomic
pointers. I'd say this is a clang bug that should be reported. – Nate Eldredge Commented Nov 19, 2024 at 4:50