c99

Variable Length Arrays in C++14?

混江龙づ霸主 提交于 2019-11-29 07:16:45
n3639 proposed the adoption of c99 's variable-length-array s into C++14 (at least for the first dimension.) But the latest I've been able to find lists n3639 as: Features in the first CD of C++14, subsequently removed to a Technical Specification Did this ever make it into a Technical Specification, or was it lost in the hand off? The reason for my question is, I've noticed this code: void f(size_t n) { int a[n]; for (size_t i = 0; i < n; ++i) a[i] = 2 * i; sort(a, a + n); } This fails to build in Visual Studio 2015 and in gcc (when the "-pedantic" flag is used.) Works fine under gcc5.1, but

A tested implementation of Peterson Lock algorithm?

孤人 提交于 2019-11-29 07:04:32
Does anyone know of a good/correct implementation of Peterson's Lock algorithm in C? I can't seem to find this. Thanks. I won't make any assertions about how good or correct the implementation is, but it was tested (briefly). This is a straight translation of the algorithm described on wikipedia. struct petersonslock_t { volatile unsigned flag[2]; volatile unsigned turn; }; typedef struct petersonslock_t petersonslock_t; petersonslock_t petersonslock () { petersonslock_t l = { { 0U, 0U }, ~0U }; return l; } void petersonslock_lock (petersonslock_t *l, int p) { assert(p == 0 || p == 1); l->flag

Do C99 signed integer types defined in stdint.h exhibit well-defined behaviour in case of an overflow?

时光怂恿深爱的人放手 提交于 2019-11-29 06:20:38
All operations on "standard" signed integer types in C (short, int, long, etc) exhibit undefined behaviour if they yield a result outside of the [TYPE_MIN, TYPE_MAX] interval (where TYPE_MIN, TYPE_MAX are the minimum and the maximum integer value respectively. that can be stored by the specific integer type. According to the C99 standard, however, all intN_t types are required to have a two's complement representation: 7.8.11.1 Exact-width integer types 1. The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two’s complement representation. Thus, int8

Kernel's “container_of” - any way to make it ISO conforming?

南笙酒味 提交于 2019-11-29 06:00:36
While looking at Linux kernel's implementation of doubly linked circular lists, I've found following macro: #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) The way this works is that it returns pointer to structure given only address of one of its members: struct blabla { int value; struct list_head *list; } Thus you can get pointer to blabla (and get to "value") given only pointer to list. To my question, how would I make this as portable as possible (best case conforming to C89/C99?).

Can GCC warn me about modifying the fields of a const struct in C99?

痞子三分冷 提交于 2019-11-29 05:57:57
问题 I stumbled upon a small issue while trying to make const-correct code. I would have liked to write a function that takes a pointer to a const struct, to tell to the compiler "please tell me if I am modifying the struct, because I really do not want to". It suddenly came to my mind that the compiler will allow me to do this: struct A { char *ptrChar; }; void f(const struct A *ptrA) { ptrA->ptrChar[0] = 'A'; // NOT DESIRED!! } Which is understandable, because what actually is const is the

32 bit Windows and the 2GB file size limit (C with fseek and ftell)

会有一股神秘感。 提交于 2019-11-29 04:52:24
I am attempting to port a small data analysis program from a 64 bit UNIX to a 32 bit Windows XP system (don't ask :)). But now I am having problems with the 2GB file size limit (long not being 64 bit on this platform). I have searched this website and others for possible sollutions but cannot find any that are directly translatable to my problem. The problem is in the use of fseek and ftell. Does anyone know of a modification to the following two functions to make them work on 32 bit Windows XP for files larger than 2GB (actually order 100GB). It is vital that the return type of nsamples is a

When __builtin_memcpy is replaced with libc's memcpy

旧城冷巷雨未停 提交于 2019-11-29 03:18:45
There is a version of C99/posix memcpy function in GCC: __builtin_memcpy . Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here : Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call. What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)? When I should prefer of using __builtin_memcpy over plain memcpy? C2H5OH I had been experimenting with the builtin

How much is it possible to create fake-functions with macros in C?

白昼怎懂夜的黑 提交于 2019-11-29 02:07:35
People always say that macros are unsafe, and also that they are not (directly) type-checking on their arguments, and so on. Worse: when errors occur, the compiler gives intrincate and incomprehensible diagnostics, because the macro is just a mess. Is it possible to use macros in almost the same way as a function, by having safe type-checking, avoiding typical pitfalls and in a way that the compiler gives the right diagnostic. I am going to answer this question (auto-answering) in an affirmative way. I want to show you the solutions that I've found to this problem. The standard C99 will be

What are “extended integer types”?

帅比萌擦擦* 提交于 2019-11-29 01:27:57
Quoting from the book I'm reading: signed char, signed short int, signed int, signed long int, signed long long int are called standard signed integer types unsigned char, unsinged short int, unsigned int, unsigned long int, unsinged long long int, _Bool are called standard unsigned integer types In addition to the standard integer types, the C99 standard allows implementation-defined extended integer types , both signed and unsigned. For example, a compiler might be provide signed and unsigned 128-bit integer types. I've problem with 3rd point. What are these "extended integer types"? Any

Why weren't new (bit width specific) printf() format option strings adoped as part of C99?

允我心安 提交于 2019-11-28 23:19:16
While researching how to do cross-platform printf() format strings in C (that is, taking into account the number of bits I expect each integer argument to printf() should be) I ran across this section of the Wikipedia article on printf() . The article discusses non-standard options that can be passed to printf() format strings, such as (what seems to be a Microsoft-specific extension): printf("%I32d\n", my32bitInt); It goes on to state that: ISO C99 includes the inttypes.h header file that includes a number of macros for use in platform-independent printf coding. ... and then lists a set of