Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning pointers to atomic type to pointers to non atomic type

Is the behavior of this code well-defined?

#include <stdatomic.h>

const int test = 42;
const int * _Atomic atomic_int_ptr;
atomic_init(&atomic_int_ptr, &test);
const int ** int_ptr_ptr = &atomic_int_ptr;
printf("int = %d\n", **int_ptr_ptr); //prints int = 42

I assigned a pointer to atomic type to a pointer to non-atomic type (the types are the same). Here are my thoughts of this example:

The Standard explicitly specify distinction of const, volatile and restrict qualifiers from the _Atomic qualifier 6.2.5(p27):

this Standard explicitly uses the phrase ‘‘atomic, qualified or unqualified type’’ whenever the atomic version of a type is permitted along with the other qualified versions of a type. The phrase ‘‘qualified or unqualified type’’, without specific mention of atomic, does not include the atomic types.

Also the compatibility of qualified types is defined as 6.7.3(p10):

For two qualified types to be compatible, both shall have the identically qualified versionof a compatible type; the order of type qualifiers within a list of specifiers or qualifiers does not affect the specified type.

Combining the quotes cited above I concluded that atomic and non-atomic types are compatible types. So, applying the rule of simple assigning 6.5.16.1(p1) (emp. mine):

the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

So I concluded that the behavior is well defined (even in spite of assigning atomic type to a non-atomic type).

The problem with all that is that applying the rules above we can also conclude that simple assignment a non-atomic type to an atomic type is also well defined which is obviously not true since we have a dedicated generic atomic_store function for that.

like image 249
Some Name Avatar asked Apr 06 '19 08:04

Some Name


People also ask

What does the “pointer to” data type mean?

The “pointer to”data type The &operand followed by a variable name returns its memory address. The data type of the result is “pointer to”followed by the type of the used variable. The rule to obtain the syntax and meaning of these data types is:

What is pointer to T in C?

For each data type Tthere exists a data type called “Pointer to T”defined as “T *”. In the following table the consequences of applying this rule to the basic C data types are shown together with some examples of variable declarations. Type T Size(bytes) [a] Pointer to T Size(bytes) Example of use int 4 int * 4

Can pointers be cast from one type to another type?

However, pointers may be type cast from one type to another type. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char. Pa is declared as a pointer to int variables, Pd is declared as a pointer to double type variables, and Pc is declared as pointer to character type variables.

Why is the size of pointers to data structures the same?

The size of pointers is always the same regardless of the data they point because all of them store a memory address. In the case of data structures, the rule applies exactly the same. The following example shows how a structure is defined and a variable and a pointer to that structure are declared:


Video Answer


1 Answers

6.2.5p27:

Further, there is the _Atomic qualifier. The presence of the _Atomic qualifier designates an atomic type. The size, representation, and alignment of an atomic type need not be the same as those of the corresponding unqualified type. Therefore, this Standard explicitly uses the phrase ''atomic, qualified or unqualified type'' whenever the atomic version of a type is permitted along with the other qualified versions of a type. The phrase ''qualified or unqualified type'', without specific mention of atomic, does not include the atomic types.

I think this should make it clear that atomic-qualified types are not deemed compatible with qualified or unqualified versions of the types they're based on.

like image 79
PSkocik Avatar answered Sep 29 '22 21:09

PSkocik