Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #389818

Re: `atomic_fetch_add` on pointer types?

Path csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: `atomic_fetch_add` on pointer types?
Date Fri, 27 Dec 2024 17:43:40 -0800
Organization None to speak of
Lines 43
Message-ID <87ttaotwdv.fsf@nosuchdomain.example.com> (permalink)
References <vkn9lk$3rtdm$1@dont-email.me>
MIME-Version 1.0
Content-Type text/plain
Injection-Date Sat, 28 Dec 2024 02:43:41 +0100 (CET)
Injection-Info dont-email.me; posting-host="474f668e455686f89f5a77fb09d755b0"; logging-data="4152344"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yNF1TxGkFM9GswIiBZC1J"
User-Agent Gnus/5.13 (Gnus v5.13)
Cancel-Lock sha1:m7i2aW3jbeVGA6bhVd5hPDC78lY= sha1:igcUZ+gyNiqFRfGVUooIzyPPemA=
Xref csiph.com comp.lang.c:389818

Show key headers only | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> The code
>
>   #include <stdatomic.h>
>   #include <stdio.h>
>
>   int main()
>   {
>     int v[2], *_Atomic p = v;
>     atomic_fetch_add(&p, 1);
>     printf("%tu\n", (char *) p - (char *) v);
>   }
>
> GCC prints 1 (https://godbolt.org/z/55Pjcnd3Y), while Clang prints 4
> (https://godbolt.org/z/e4x6z85fe). Obviously, Clang performed the
> "proper" pointer arithmetic, while GCC ignored the pointer type.
>
> Which one is correct here?

See also the thread in comp.std.c, where I wrote that I think you've
found an error in the standard.  (Probably just one newsgroup or the
other would have been more appropriate.)

IMHO neither is correct.  The description clearly restricts the
operations to atomic integer types.  The mention of "address types"
appears to be an error.

Perhaps the intent was to allow just _add and _sub on atomic pointer
types, but the standard doesn't express that intent.  If the intent
is to allow those operations on pointers, it seems clear that clang's
interpretation would be correct; pointer arithmetic counts elements,
not bytes.

Note that if you change atomic_fetch_add to _or, _and, or _xor, gcc
doesn't complain, which seems clearly incorrect.  clang correctly
complains "address argument to atomic operation must be a pointer
to atomic integer", but it quietly accepts _add and _sub.

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

`atomic_fetch_add` on pointer types? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-12-27 14:28 -0800
  Re: `atomic_fetch_add` on pointer types? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-27 17:43 -0800
  Re: `atomic_fetch_add` on pointer types? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-12-27 20:12 -0800
  Re: `atomic_fetch_add` on pointer types? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-12-31 06:08 -0800

csiph-web