c11

How to initialize a char array without the null terminator?

好久不见. 提交于 2020-07-20 10:53:14
问题 The char array is a part of network message, which has well defined length, so the null terminator is not needed. struct Cmd { char cmd[4]; int arg; } struct Cmd cmd { "ABCD" , 0 }; // this would be buffer overflow How can I initialize this cmd member char array? without using functions like strncpy ? 回答1: Terminating null character is ignored if the size of the char array is the same as the number of characters in the initializer. So cmd will not have the null terminator. The relevant

How to initialize a char array without the null terminator?

[亡魂溺海] 提交于 2020-07-20 10:52:31
问题 The char array is a part of network message, which has well defined length, so the null terminator is not needed. struct Cmd { char cmd[4]; int arg; } struct Cmd cmd { "ABCD" , 0 }; // this would be buffer overflow How can I initialize this cmd member char array? without using functions like strncpy ? 回答1: Terminating null character is ignored if the size of the char array is the same as the number of characters in the initializer. So cmd will not have the null terminator. The relevant

Is clock_gettime() correctly implemented in MinGW GCC 8.2.0?

我们两清 提交于 2020-06-29 04:04:16
问题 By chance, I found out about the existence of the clock_gettime() function for Linux systems. Since I'm looking for a way to measure execution time of a function, I tried it in the MinGW gcc 8.2.0 version on a Windows 10 64-bit machine: #include <time.h> #include <stdio.h> int main() { struct timespec tstart, tend; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tstart); for (int i = 0; i < 100000; ++i); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tend); printf("It takes %li nanoseconds for 100,000

stdatomic (C11), three questions about _Atomic types

喜夏-厌秋 提交于 2020-06-28 06:37:29
问题 First question I found on cppreference _Atomic ( type-name ) (since C11) Use as a type specifier; this designates a new atomic type _Atomic type-name (2) (since C11) Use as a type qualifier; this designates the atomic version of type-name. In this role, it may be mixed with const, volatile, and restrict), although unlike other qualifiers, the atomic version of type-name may have a different size, alignment, and object representation. So does using _Atomic(int) instead of _Atomic int guarantee

Use of comma in a typedef declaration?

与世无争的帅哥 提交于 2020-06-27 15:10:19
问题 Is this declaration C99/C11 compliant ? typedef struct element { char *data; struct element* next; } element, *list, elements[5]; I could not find why it works in the standard. 回答1: Yes, it is standard compliant. typedef declarations are like normal declarations except the identifiers declared by them become type aliases for the type of object the identifier would be if the declaration had no typedef in it. So while int integer, *pointer_to_integer; declare an int object named integer and an

Why is there no 'aligned_realloc' on most platforms?

江枫思渺然 提交于 2020-06-12 04:42:23
问题 MSVC has its own non-standard functions _aligned_malloc , _aligned_realloc and _aligned_free . C++17 and C11 have introduced (std::)aligned_alloc , results of which can be de allocated with free or realloc . But realloc cannot be used to actually re allocate memory returned by aligned_alloc , since it does not take an alignment parameter and thus cannot guarantee that the returned pointer will be properly aligned. I can't even find any non-standard extensions that could reallocate aligned

Why is there no 'aligned_realloc' on most platforms?

我们两清 提交于 2020-06-12 04:41:56
问题 MSVC has its own non-standard functions _aligned_malloc , _aligned_realloc and _aligned_free . C++17 and C11 have introduced (std::)aligned_alloc , results of which can be de allocated with free or realloc . But realloc cannot be used to actually re allocate memory returned by aligned_alloc , since it does not take an alignment parameter and thus cannot guarantee that the returned pointer will be properly aligned. I can't even find any non-standard extensions that could reallocate aligned

Why does a function prototype with an empty argument list conflicts with one that has a char argument?

大兔子大兔子 提交于 2020-05-29 04:05:44
问题 With the code below, both clang and gcc called with -std=c11 complain about conflicting types for foo. int foo(); int main(void) { return foo(0); } int foo(char a) { return a; } According to the answer in https://stackoverflow.com/a/13950800/1078224, in (older?) C standards the type int has been assumed when no variable type was given. However, the C11 standard draft (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 6.7.6.3, $14 says that The empty list in a function