2

First question

I found on cppreference

  • _Atomic ( type-name ) (since C11)

    Use as a type specifier; this designates a new atomic type

  • _Atomic type-name (2) (since C11)

    Use as a type qualifier; this designates the atomic version of type-name. In this role, it may be mixed with const, volatile, and restrict), although unlike other qualifiers, the atomic version of type-name may have a different size, alignment, and object representation.

So does using _Atomic(int) instead of _Atomic int guarantee it to be the same size as int or not?

Second question

Using a qualifier inside _Atomic Ex:

_Atomic(volatile int)

Throws an error, but using it like this:

_Atomic(volatile _Atomic(int)*)

Does not; is this standard behaviour?

Last question

I noticed atomic functions (ex: atomic_store, atomic_load, atomic_compare_exchange_weak) work without the passed types being _Atomic types, and I can still manage race conditions with no problem. Is this standard behaviour? Does it have downsides or lead to any error?

Alex Sim
  • 403
  • 3
  • 16
  • 1
    What do you mean by "I noticed atomic functions (ex: atomic_store, atomic_load, atomic_compare_exchange_weak) work without the passed types being _Atomic types"? According to C11 draft for example [`atomic_load()`](http://port70.net/~nsz/c/c11/n1570.html#7.17.7.2) expects a single argument that is a pointer to atomic type. – Ilja Everilä Dec 26 '17 at 15:17
  • "throws an error" - what error does it throw?! – Antti Haapala -- Слава Україні Dec 26 '17 at 15:25
  • @Ilja Everilä I'm passing any kind of pointer to it, still working as it should – Alex Sim Dec 26 '17 at 15:36
  • In C a lot of times things might seem to work, until they don't, if you invoke UB. – Ilja Everilä Dec 26 '17 at 15:49
  • Please don't ask several questions at once, really. This makes useful searching much more difficult. You could even split it up, now, given them useful question titles, and answer them yourself with what you now know from Antti's answer. – Jens Gustedt Dec 26 '17 at 16:51

1 Answers1

5

First question:

C11 7.17.6p3:

NOTE The representation of atomic integer types need not have the same size as their corresponding regular types. They should have the same size whenever possible, as it eases effort required to port existing code.

Second question:

C11 6.7.2.4p3:

[Constraints]

3 The type name in an atomic type specifier shall not refer to an array type, a function type, an atomic type, or a qualified type.

volatile int is a qualified type. A shall in a constraints section is violated, therefore the compiler needs to output a diagnostics message. Beyond that, the behaviour of such a construct is undefined.

Third question:

C11 7.17.1.p5:

5 In the following synopses:

  • An A refers to one of the atomic types.

They expect an _Atomic type. You pass in a non-atomic variable, therefore undefined behaviour.

Community
  • 1
  • 1