c11

Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis

旧街凉风 提交于 2019-12-24 00:58:00
问题 I know that declaring a function (or function pointer) with no parameter list (and without specifying void in the parameter list) that means that the function (or function pointer) has an unknown number of arguments. I wrote some test scripts to check this behavior out: int my_func_1() { return(0); } int my_func_2(int x) { return(x); } int my_func_3(char x, ...) { va_list va; va_start(va, x); return(va_arg(va, int)); } int (*fp)(); fp = my_func_1; printf("%d\n", fp()); fp = my_func_2; printf(

Current C11 Implementation Status (<threads.h>)?

試著忘記壹切 提交于 2019-12-23 20:31:18
问题 I'm curious what the status of C11 implementations are, specifically in regard to the optional <threads.h> . Do any platforms currently support the interfaces? If not, are there any plans to implement the interfaces in terms of POSIX and/or WinAPI? Is anyone else planning on using the interfaces described in C11 ( thrd_t , mtx_t , cond_t , etc...)? 来源: https://stackoverflow.com/questions/23400686/current-c11-implementation-status-threads-h

thrd_busy and mtx_lock()/mtx_timedlock()

不羁的心 提交于 2019-12-23 13:08:59
问题 I have the following questions about C1x mutexes (§7.25.4): In which situations can mtx_lock() return thrd_busy instead of blocking? In which situations can mtx_timedlock() return thrd_busy ? Note that thrd_busy is defined in §7.25.1 ¶5 as being returned " when a resource requested by a test and return function is already in use ". I would expect thrd_busy to be only returned by mtx_trylock() , or at most also by mtx_lock() when invoked with a mtx_try or mtx_try | mtx_recursive mutex, but

Is there a strlen() that works with char16_t?

我只是一个虾纸丫 提交于 2019-12-23 08:26:09
问题 As the question says: typedef __CHAR16_TYPE__ char16_t; int main(void) { static char16_t test[] = u"Hello World!\n"; printf("Length = %d", strlen(test)); // strlen equivalent for char16_t ??? return 0; } I searched and found only C++ solutions. My compiler is GCC 4.7 . Edit: To clarify, I was searching for a solution that returns the count of code points , not the count of characters . These two are quite different for UTF-16 strings containing characters outside the BMP . 回答1: Here's your

Is there a strlen() that works with char16_t?

一笑奈何 提交于 2019-12-23 08:26:05
问题 As the question says: typedef __CHAR16_TYPE__ char16_t; int main(void) { static char16_t test[] = u"Hello World!\n"; printf("Length = %d", strlen(test)); // strlen equivalent for char16_t ??? return 0; } I searched and found only C++ solutions. My compiler is GCC 4.7 . Edit: To clarify, I was searching for a solution that returns the count of code points , not the count of characters . These two are quite different for UTF-16 strings containing characters outside the BMP . 回答1: Here's your

How to include C11 headers when compiling C++ with GCC?

天大地大妈咪最大 提交于 2019-12-22 11:35:52
问题 In a C++ project, I'm using a C library which includes some C11 headers. It won't compile with GCC. See this simple code: // main.cc #include <stdatomic.h> int main() { return 0; } Running gcc main.cc -lstdc++ , it complains: error: ‘_Atomic’ does not name a type . However, clang main.cc -lstdc++ works like a charm. I'm wondering what makes the difference, and how can I compile it with gcc? 回答1: To wrap C headers that use atomics, you may use the other spelling of _Atomic and define a macro

About the array subscripting operator

别说谁变了你拦得住时间么 提交于 2019-12-22 05:37:12
问题 Quoting from the C11 standard : Array subsripting (§ 6.5.2.1) The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) . I would like to know why are the brackets around E1 necessary (they were missing in the C89 standard), ie in which expression can (*(E1+(E2))) be different from (*((E1)+(E2))) ? 回答1: According to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n841.htm, it is inserted just for clarity. The two expressions are the syntatically equivalent.

__STDC_LIB_EXT1__ availability in gcc and clang

僤鯓⒐⒋嵵緔 提交于 2019-12-22 05:10:55
问题 Since a quick Google search did not find anything, I will try to ask here (since many people involved in gcc/clang hang around here) - What is the status of __STDC_LIB_EXT1__ in gcc/clang? We are developing a cross platform applicataion and I wanted to use some of the safe bounds checking functions from <stdio.h> (which by miracle are available on Visual Studio 2017), but could not compile the code with Xcode 9.2. I assumed maybe the clang version Xcode uses is outdated, but gcc 6.3.0 on

Is it legal to call memchr with a too-long length, if you know the character will be found before reaching the end of the valid region?

南楼画角 提交于 2019-12-22 04:18:31
问题 Is the following defined behavior in C11 and C++11 1 ? bool has4() { char buf[10] = {0, 1, 2, 4}; return memchr(buf, 4, 20); } Here we pass a too-long length to memchr . The array has 10 elements but we pass 20. The element we are searching for, however, is always found before the end. It is clear to me if this is legal. If this is allowed, it would limit implementation flexibility, since the implementation cannot rely on the size being a valid indication of the size of the accessible memory

C11 type-generic expressions - why not just add function overloading?

。_饼干妹妹 提交于 2019-12-22 01:33:07
问题 I was just reading the Wikipedia article on C11, the new version of the C standard released in Dec 2011, and I saw that one of the added features was "type-generic expressions": Type-generic expressions using the _Generic keyword. For example, the following macro cbrt(x) translates to cbrtl(x) , cbrt(x) or cbrtf(x) depending on the type of x : #define cbrt(X) _Generic((X), long double: cbrtl, \ default: cbrt, \ float: cbrtf)(X) This looks pretty horrible to me - if they are going to change