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.org/z/fMqzoe4b5
Does clang support _Atomic Pointer Arithmetic
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With