c11

Is using any indeterminate value undefined or just those stored in objects with automatic storage?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:47:21
问题 According to C99 J.2, the behavior is undefined when: The value of an object with automatic storage duration is used while it is indeterminate What about all the other cases where an object has an indeterminate value? Do we also always invoke UB if we use them? Or do we invoke UB only when they contain a trap representation? Examples include: the value of an object allocated using malloc (7.20.3.3p2) [storing in non-automatic storage] a FILE* after calling fclose on it (7.19.3p4) [storing in

Can a standards-conforming string be longer than SIZE_MAX characters?

前提是你 提交于 2019-12-10 15:39:54
问题 I did not find anything in the C11 standard stating that a string could not be longer than SIZE_MAX (where SIZE_MAX denotes the maximum value of the size_t type) characters. E.g. if size_max is long , and in my implementation there is a long long type that is strictly larger than long , then I could define and index such a string using a long long . However, this would imply some unusual situations: strlen , for instance, might be unable to return the actual size of the string, since the

How to understand atomics in a freestanding C or C++ implementation? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:06:14
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . C11 and C++11 define atomics in terms of threads of execution. Whereas in a hosted environment it is clear what a thread is, it is rather a vague term in a freestanding language implementation. How to formally understand atomics specified in C11 and C++11 in a freestanding

Strange wording in the standard, concerning comparison of pointers

独自空忆成欢 提交于 2019-12-10 14:48:44
问题 §6.5.8\6 (concerning >, <, <=, >=) If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P . In all other cases, the behavior is undefined. Several sections above, §6.5.8, it is explained that basically, pointer arithmetic work as expected on arrays. That is int a[3]; int *p = a; int *q = &a[2]; //q-p == 3 is valid. However, as I read the above q > p is UB. What am

Unions and strict aliasing in C11

旧时模样 提交于 2019-12-10 13:29:57
问题 Assuming I have a union like this union buffer { struct { T* data; int count; int capacity; }; struct { void* data; int count; int capacity; } __type_erased; }; Will I get into trouble if I mix reads/writes to the anonymous struct members and __type_erased members under C11 aliasing rules? More specifically, I am interested in the behaviour that occurs if the components are accessed independently (e.g. via different pointers). To illustrate: grow_buffer(&buffer.__type_erased); buffer.data

Detecting if a macro argument is a typename

删除回忆录丶 提交于 2019-12-10 12:53:04
问题 Within C11/gnuC11 is it possible to write a macro that returns an integer constant expression of value 1 or 0 respectively if the macro argument is or isn't a type name or at least a macro can distinguish between integer constant expressions and typenames (i.e., if can detect the argument isn't one of these, it can assume it is the other)? #define IS_TYPENAME(X) /*???*/ _Static_assert( IS_TYPENAME(int), "" ); _Static_assert( !IS_TYPENAME(42), "" ); Motivation: My motivation was to wrap

Possible to use C11 fences to reason about writes from other threads?

无人久伴 提交于 2019-12-10 11:19:28
问题 Adve and Gharachorloo's report, in Figure 4b, provides the following example of a program that exhibits unexpected behavior in the absence of sequential consistency: My question is whether it is possible, using only C11 fences and memory_order_relaxed loads and stores, to ensure that register1, if written, will be written with the value 1. The reason this might be hard to guarantee in the abstract is that P1, P2, and P3 could be at different points in a pathological NUMA network with the

Forcing compiler to C99 standard

流过昼夜 提交于 2019-12-10 03:31:55
问题 I was coding on my project when I discovered that the anonymous structs I've been using for a while are actually only available in C11, not C99, the standard I want to code against. Given the following code: struct data { int a; struct { int b; int c; }; }; int main() { struct data d; d.a = 0; d.b = 1; d.c = 2; return 0; } This code should only compile in C11 (or if compiler extensions provide this feature and are enabled). So let's see the results on different compilers: clang 5 compiler:

Do unnamed bit-fields have well-defined semantics?

↘锁芯ラ 提交于 2019-12-10 01:50:15
问题 Is the following code guaranteed to terminate normally and successfully? #include <assert.h> struct foo_s { union { struct { unsigned a : 10; unsigned : 6; }; struct { unsigned : 10; unsigned b : 6; }; struct { unsigned : 10; unsigned c : 6; }; }; }; int main () { struct foo_s f; f.a = 0; f.b = 1; assert(f.a == 0); return 0; } While answering a different question, the possibility was raised that assignment to a named bit-field in a structure that also contains an unnamed bit-field may cause

Why cannot C type-generic expressions be compatible with C++?

放肆的年华 提交于 2019-12-09 15:42:12
问题 I seem to recall hearing vague comments from a few reliable sources (i.e. committee members speaking in non-official channels) that C type-generic expressions will not be added to C++ because they cannot be. As far as I can tell, type-generic expressions are very limited compared to C++ templates and overloading, but there is no potential for interaction that would need to be defined as a special case. A type-generic expression consists of a controlling expression, and a series of