最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Does clang support _Atomic Pointer Arithmetic - Stack Overflow

programmeradmin4浏览0评论

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
  • a++ succeeds, as does atomic_fetch_add(&a, n). – Nate Eldredge Commented Nov 19, 2024 at 4:47
  • I'm pretty sure the C standard requires a += 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
  • Oh, I see you already did: github/llvm/llvm-project/issues/116736 – Nate Eldredge Commented Nov 19, 2024 at 4:59
Add a comment  | 

1 Answer 1

Reset to default 4

This 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.

发布评论

评论列表(0)

  1. 暂无评论