c11

Potential problem with C standard malloc'ing chars

本小妞迷上赌 提交于 2019-11-29 15:09:17
问题 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,

Can a char array be used with any data type?

妖精的绣舞 提交于 2019-11-29 14:49:48
问题 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

Does x86-SSE-instructions have an automatic release-acquire order?

此生再无相见时 提交于 2019-11-29 13:51:33
As we know from from C11-memory_order: http://en.cppreference.com/w/c/atomic/memory_order And the same from C++11-std::memory_order: http://en.cppreference.com/w/cpp/atomic/memory_order On strongly-ordered systems ( x86 , SPARC, IBM mainframe), release-acquire ordering is automatic. No additional CPU instructions are issued for this synchronization mode , only certain compiler optimizations are affected (e.g. the compiler is prohibited from moving non-atomic stores past the atomic store-release or perform non-atomic loads earlier than the atomic load-acquire) But is this true for x86-SSE

Why doesn't the compiler detect and produce errors when attempting to modify char * string literals?

隐身守侯 提交于 2019-11-29 11:13:29
Assume the following two pieces of code: char *c = "hello world"; c[1] = 'y'; The one above doesn't work. char c[] = "hello world"; c[1] = 'y'; This one does. With regards to the first one, I understand that the string "hello world" might be stored in the read only memory section and hence can't be changed. The second one however creates a character array on the stack and hence can be modified. My question is this - why don't compilers detect the first type of error? Why isn't that part of the C standard? Is there some particular reason for this? C compilers are not required to detect the

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

核能气质少年 提交于 2019-11-29 10:53:15
问题 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

C11 grammar ambiguity between _Atomic type specifier and qualifier

拜拜、爱过 提交于 2019-11-29 09:35:59
I'm trying to write a lex/yacc grammar for C11 based off of N1570. Most of my grammar is copied verbatim from the informative syntax summary, but some yacc conflicts arose. I've managed to resolve all of them except for one: there seems to be some ambiguity between when '_Atomic' is used as a type specifier and when it's used as a type qualifier. In the specifier form, _Atomic is followed immediately by parentheses, so I'm assuming it has something to do with C's little-used syntax which allows declarators to be in parentheses, thus allowing parentheses to immediately follow a qualifier. But

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

↘锁芯ラ 提交于 2019-11-29 05:25:32
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 space of the program, or a null pointer, […]) […] the behavior is undefined. […] The standard function

How to enable c11 on later versions of gcc?

限于喜欢 提交于 2019-11-29 02:49:56
I currently use gcc 4.6.3 . My understanding is that gcc by default uses the gnu89 standard and I would like to enable C11, the latest C standard. I tried: [pauldb@pauldb-laptop test ]$ gcc -std=c11 -o test test.c cc1: error: unrecognised command line option ‘-std=c11’ I replaced c11 with gnu11 and I get the same error. What is the correct way to enable the latest C standard for gcc? (Note: I'm interested in the latest C standard and not the latest C++ one.) ouah The correct option is -std=c11 . However, it is not available in gcc 4.6 . You need at least gcc 4.7 to have this option supported.

What is the __STDC_VERSION__ value for C11?

廉价感情. 提交于 2019-11-29 01:02:06
I know that compilers use __STDC__ to indicate that a compiler is standard C and, from, there, you can use __STDC_VERSION__ to figure out which level of the standard you're using. I also know that C90 had no value, C90 amendment 1 had 199401L and C99 had 199901L . The latest C1x draft I have simply states it as 201ymmL and I'm assuming it was made a less "vague" value in the final standard. My guess is that it will be 201112L since that's when C11 was ratified but I'd like to be certain. I thought I could try using gcc -std=c1x but the version of gcc I'm running doesn't support that yet. Does

initialization of anonymous structures or unions in C1X

柔情痞子 提交于 2019-11-28 23:14:19
I have the following question: How are anonymous structures (or unions) properly initialized according to the current C1X draft ? Is this legal: struct foo { int a; struct { int i; int j; }; int b; }; struct foo f = { 1, 2, 3, 4 }; struct foo g = { 1, { 2 }, 3 }; In GCC, g.j == 0 and g.b == 3 , while in tcc g.j == 3 and g.b == 0 . The current draft says: "[...] unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.". Can this be really true? Isn't struct foo h = { 0 };