c11

Potential problem with C standard malloc'ing chars

我是研究僧i 提交于 2019-11-30 09:32:44
When answering a comment to another answer of mine here , I found what I think may be a hole in the C standard (c1x, I haven't checked the earlier ones and yes, I know it's incredibly unlikely that I alone among all the planet's inhabitants have found a bug in the standard). Information follows: Section 6.5.3.4 ("The sizeof operator") para 2 states "The sizeof operator yields the size (in bytes) of its operand" . Para 3 of that section states: "When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1" . Section 7.20.3.3

Sequence points and side effects: Quiet change in C11?

孤人 提交于 2019-11-30 08:35:17
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 floating-point status flag is not an object and can be set more than once within an expression. 73)

Memory position of elements in C/C++ union

烈酒焚心 提交于 2019-11-30 08:26:45
I have a union in C like this: union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp_num; }; My question is, can I guarantee that if given the following: union AUnion u; Then the following are true: &u == &u.num &u == &u.fp_num &u == &u.charbuf I.e they all start at the beginning of the memory segment where u is stored. In the case of this C program compiled with gcc version 5.3.0 and -std=c11 the above is true: #include <stdio.h> #include <stdint.h> #include <stdlib.h> union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp

Can a void-returning function g return f(); when f returns void?

别说谁变了你拦得住时间么 提交于 2019-11-30 08:00:35
Consider the following snippet: void f(void); void g(…) { … return f(); … } Is this return f(); valid according to C11? I am not advocating using this pattern: if it works at all, it is obviously equivalent to f(); return; (where the return; itself would be redundant if this is at the end of function g() ). I am asking this question in the context of the static analysis of C programs, where the C code has already been written by someone else and the question is deciding whether or not it is valid according to the standard. I would interpret C11 6.8.6.4:1 as meaning that it is non-standard and

Is memcpy(&a + 1, &b + 1, 0) defined in C11?

余生颓废 提交于 2019-11-30 07:45:14
问题 This question follows this previous question about the definedness of memcpy(0, 0, 0) , which has been conclusively determined to be undefined behavior. As the linked question shows, the answer hinges on the contents of C11's clause 7.1.4:1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address

Latest changes in C11

旧城冷巷雨未停 提交于 2019-11-30 06:22:00
问题 C1x has become ISO/IEC 9899:2011 aka C11. Does anyone know what changes (if any) there are in the Standard from the April 2011 draft n1570? ETA: There are the Committee minutes from London (March 2011) (which should be included in n1570) here, and from Washington, DC (October 2011) here; I suppose a list of accepted changes in the DC minutes should cover things. 回答1: I just learned today that there was one (somewhat) significant change between N1570 and the final C11 standard (ISO/IEC 9899

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

二次信任 提交于 2019-11-30 05:44:51
问题 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

Why does “noreturn” function return?

故事扮演 提交于 2019-11-30 02:34:21
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 Why does function func() return after providing noreturn attribute? Sourav Ghosh The function

Is there a way to use GCC __attribute__((noreturn)) and <stdnoreturn.h> sanely in a single translation unit?

余生颓废 提交于 2019-11-29 18:56:04
问题 In C11, there is the keyword _Noreturn which is a function specifier (like inline is) that indicates the function does not return — it calls exit() or equivalent. There is also a header, <stdnoreturn.h> , the complete definition for which is: 7.23 _Noreturn <stdnoreturn.h> ¶1 The header <stdnoreturn.h> defines the macro noreturn which expands to _Noreturn . In GNU C (GCC), there is an attribute __attribute__((noreturn)) that does essentially the same job. One difference is that __attribute__(

<stdatomic.h> in GCC 4.8?

为君一笑 提交于 2019-11-29 16:38:50
问题 I'd like to make use of the new atomic operations provided by the C11 standard. However, trying to #include the appropriate header file gives me this: csort-par.c:5:23: fatal error: stdatomic.h: No such file or directory #include <stdatomic.h> The documentation at http://gcc.gnu.org/wiki/C11Status seems to say that the header file has been provided since GCC 4.7... am I missing something? __STDC_NO_ATOMICS__ is not defined. gcc --version is as follows: gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8