c11

Can DMB instructions be safely omitted in ARM Cortex M4

这一生的挚爱 提交于 2019-12-01 12:38:24
I am going through the assembly generated by GCC for an ARM Cortex M4, and noticed that atomic_compare_exchange_weak gets two DMB instructions inserted around the condition (compiled with GCC 4.9 using -std=gnu11 -O2 ): // if (atomic_compare_exchange_weak(&address, &x, y)) dmb sy ldrex r0, [r3] cmp r0, r2 itt eq strexeq lr, r1, [r3] cmpeq.w lr, #0 dmb sy bne.n ... Since the programming guide to barrier instructions for ARM Cortex M4 states that: Omitting the DMB or DSB instruction in the examples in Figure 41 and Figure 42 would not cause any error because the Cortex-M processors: do not re

Can DMB instructions be safely omitted in ARM Cortex M4

感情迁移 提交于 2019-12-01 11:39:14
问题 I am going through the assembly generated by GCC for an ARM Cortex M4, and noticed that atomic_compare_exchange_weak gets two DMB instructions inserted around the condition (compiled with GCC 4.9 using -std=gnu11 -O2 ): // if (atomic_compare_exchange_weak(&address, &x, y)) dmb sy ldrex r0, [r3] cmp r0, r2 itt eq strexeq lr, r1, [r3] cmpeq.w lr, #0 dmb sy bne.n ... Since the programming guide to barrier instructions for ARM Cortex M4 states that: Omitting the DMB or DSB instruction in the

Can the address of a variable with automatic storage duration be taken in its definition?

▼魔方 西西 提交于 2019-12-01 06:06:57
问题 Is it allowed to take the address of an object on the right hand-side of its definition, as happens in foo() below: typedef struct { char x[100]; } chars; chars make(void *p) { printf("p = %p\n", p); chars c; return c; } void foo(void) { chars b = make(&b); } If it is allowed, is there any restriction on its use, e.g., is printing it OK, can I compare it to another pointer, etc? In practice it seems to compile on the compilers I tested, with the expected behavior most of the time (but not

Clarification on an example of unions in C11 standard

不打扰是莪最后的温柔 提交于 2019-12-01 03:39:30
The following example is given in the C11 standard, 6.5.2.3 The following is not a valid fragment (because the union type is not visible within function f): struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *p1, struct t2 *p2) { if (p1->m < 0) p2->m = -p2->m; return p1->m; } int g() { union { struct t1 s1; struct t2 s2; } u; /* ... */ return f(&u.s1, &u.s2); } Why does it matter that the union type is visible to the function f? In reading through the relevant section a couple times, I could not see anything in the containing section disallowing this. It matters because of 6.5.2.3

Atomicity of the simple assignment operator

这一生的挚爱 提交于 2019-12-01 02:15:56
问题 C11 Standard says that for atomic types (_Atomic), prefix and postfix ++ and -- operations are atomic (6.5.2.4.,p2), as are compound assignments: op= (6.5.16.2,p3). I haven't found anything written about a simple assignment = . Is it also atomic? Let's says E1, E2 are int , but only E1 is defined with the specifier _Atomic. My assumption is that this: E1 = E2; is equivalent to: atomic_store( &E1 , E2 ); It my assumption correct? 回答1: Following the example in this Dr Dobbs article, simple

What is the rationale for one past the last element of an array object?

空扰寡人 提交于 2019-11-30 22:29:46
According to N1570 (C11 draft) 6.5.6/8 Additive operators : Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object , and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object Subclause 6.5.6/9 also contains: Moreover, if the expression P points either to an element of an array object or one past the last element of an array object, and the expression Q points to the last element of the same array object, the

Sequence points and side effects: Quiet change in C11?

旧城冷巷雨未停 提交于 2019-11-30 13:48:40
问题 C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 72) Furthermore, the prior value shall be read only to determine the value to be stored. 73) with the footnotes 72) A

How to declare a variable as thread local portably?

谁说胖子不能爱 提交于 2019-11-30 13:08:39
C11 introduces the _Thread_local storage class specifier that can be used in combination with the static and extern storage class specifiers to declare a variable as thread local. The GNU C compiler suite implements a storage class specifier __thread with the same same semantics. Unfortunately I did not find any compiler (I tried gcc, clang and SUN studio) that actually implements the _Thread_local keywords. I currently use the following construct to declare a keyword thread_local : /* gcc doesn't know _Thread_local from C11 yet */ #ifdef __GNUC__ # define thread_local __thread #elif __STDC

Why does “noreturn” function return?

房东的猫 提交于 2019-11-30 12:22:18
问题 I read this question about noreturn attribute, which is used for functions that don't return to the caller. Then I have made a program in C. #include <stdio.h> #include <stdnoreturn.h> noreturn void func() { printf("noreturn func\n"); } int main() { func(); } And generated assembly of the code using this: .LC0: .string "func" func: pushq %rbp movq %rsp, %rbp movl $.LC0, %edi call puts nop popq %rbp ret // ==> Here function return value. main: pushq %rbp movq %rsp, %rbp movl $0, %eax call func

Can a char array be used with any data type?

佐手、 提交于 2019-11-30 09:40:11
The malloc() function returns a pointer of type void* . It allocates memory in bytes according to the size_t value passed as argument to it. The resulting allocation is raw bytes which can be used with any data type in C(without casting). Can an array with type char declared within a function that returns void * , be used with any data type like the resulting allocation of malloc ? For example, #include <stdio.h> void *Stat_Mem(); int main(void) { //size : 10 * sizeof(int) int buf[] = { 1,2,3,4,5,6,7,8,9,10 }; int *p = Stat_Mem(); memcpy(p, buf, sizeof(buf)); for (int n = 0; n < 10; n++) {