ISO C: Difference between revisions

From dankwiki
 
(7 intermediate revisions by the same user not shown)
Line 6: Line 6:
* I'd like to further along [http://gcc.gnu.org/c99status.html gcc C99 support]
* I'd like to further along [http://gcc.gnu.org/c99status.html gcc C99 support]


==Threads==
==Concurrency==
* Hans Boehm's [http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/ Threads and Memory Model for C++] page is awesome
* Hans Boehm's [http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/ Threads and Memory Model for C++] page is awesome


==Aliasing==
===Atomics===
Introduced in C11, and exposed by <tt>stdatomic.h</tt>. Use <tt>_Atomic</tt> as a type specifier (ala <tt>volatile</tt> or <tt>const</tt>).
* Unlike other type specifiers, an atomic form of a base type might have different size/alignment than the base type.
* <tt>struct</tt>s and <tt>union</tt>s may be declared atomic, but it is then undefined behavior to access any of their members.
* Implementations are "encouraged to ensure that representation of C and C++ atomic types is the same."
 
==Asserts==
* <tt>_Static_assert()</tt> is evaluated at compile time, huzzah!
 
==Generic dispatch==
The new <tt>_Genetic</tt> keyword allows mapping the type of an expression to a set of expressions. This is mainly usable for type-based dispatch of the kind seen in <tt>tgmath.h</tt>. You can't, so far as I can tell, use it for true parametric polymorphism. Tony Finch demonstrates a clever use for [https://fanf.livejournal.com/144696.html parametric constness]:
<code>
    #define strchr(s,c) _Generic((s),                    \
        const char * : (const char *)(strchr)((s), (c)), \
        char *      :              (strchr)((s), (c)))</code>
 
==Types==
===Aliasing===
* See [[Compiler Design]] page
* See [[Compiler Design]] page
* -O2 implies -fstrict-aliasing, at least as of gcc 4.3
* -O2 implies -fstrict-aliasing, at least as of gcc 4.3
Line 15: Line 32:
** How to use Berkeley sockets API? See http://archives.free.net.ph/message/20080529.200047.b40321b6.fi.html, etc
** How to use Berkeley sockets API? See http://archives.free.net.ph/message/20080529.200047.b40321b6.fi.html, etc


==stdint.h==
 
===stdint.h===
An excellent addition in C99. Type constructions of the form u?int{variety}_t (u prefix denotes unsigned). Varieties include:
An excellent addition in C99. Type constructions of the form u?int{variety}_t (u prefix denotes unsigned). Varieties include:
* <tt>ptr</tt>: Size sufficient to hold a pointer. This is useful for function type definitions, when it's unsure whether a pointer or integer type would be most appropriate for various instances (a <tt>void *</tt>'s size is not directly related to an <tt>int</tt>'s size by the ANSI C standard).
* <tt>ptr</tt>: Size sufficient to hold a pointer. This is useful for function type definitions, when it's unsure whether a pointer or integer type would be most appropriate for various instances (a <tt>void *</tt>'s size is not directly related to an <tt>int</tt>'s size by the ANSI C standard).
Line 22: Line 40:
* <tt>least{8,16,32,64}</tt>: Minimum native integer type having at least the specified width.
* <tt>least{8,16,32,64}</tt>: Minimum native integer type having at least the specified width.
* <tt>{8,16,32,64}</tt>: Integer type having precisely this width.
* <tt>{8,16,32,64}</tt>: Integer type having precisely this width.
====inttypes.h====
ugly <tt>printf</tt> specifiers for the types in <tt>stdint.h</tt>. e.g.:
* <tt>printf("%"PRIu64"\n", some_uint64_tvar);</tt>
===tgmath.h===
===<tt>static</tt> in array parameters===
Beginning with C99, <tt>static</tt> can be inserted into array parameters ala:
<code>
void foo(int bararr[static 10]);
</code>
This indicates to the compiler that the array passed as <tt>bararr</tt> must have room for at least 10 elements. This ought help it produce diagnostics, and perhaps help code generation.

Latest revision as of 05:36, 29 July 2021

Compilers

gcc

Concurrency

Atomics

Introduced in C11, and exposed by stdatomic.h. Use _Atomic as a type specifier (ala volatile or const).

  • Unlike other type specifiers, an atomic form of a base type might have different size/alignment than the base type.
  • structs and unions may be declared atomic, but it is then undefined behavior to access any of their members.
  • Implementations are "encouraged to ensure that representation of C and C++ atomic types is the same."

Asserts

  • _Static_assert() is evaluated at compile time, huzzah!

Generic dispatch

The new _Genetic keyword allows mapping the type of an expression to a set of expressions. This is mainly usable for type-based dispatch of the kind seen in tgmath.h. You can't, so far as I can tell, use it for true parametric polymorphism. Tony Finch demonstrates a clever use for parametric constness:

   #define strchr(s,c) _Generic((s),                    \
       const char * : (const char *)(strchr)((s), (c)), \
       char *       :               (strchr)((s), (c)))

Types

Aliasing


stdint.h

An excellent addition in C99. Type constructions of the form u?int{variety}_t (u prefix denotes unsigned). Varieties include:

  • ptr: Size sufficient to hold a pointer. This is useful for function type definitions, when it's unsure whether a pointer or integer type would be most appropriate for various instances (a void *'s size is not directly related to an int's size by the ANSI C standard).
  • max: Size sufficient to hold any other integer type.
  • fast{8,16,32,64}: Fastest integer type having at least the specified width.
  • least{8,16,32,64}: Minimum native integer type having at least the specified width.
  • {8,16,32,64}: Integer type having precisely this width.

inttypes.h

ugly printf specifiers for the types in stdint.h. e.g.:

  • printf("%"PRIu64"\n", some_uint64_tvar);

tgmath.h

static in array parameters

Beginning with C99, static can be inserted into array parameters ala:

void foo(int bararr[static 10]);

This indicates to the compiler that the array passed as bararr must have room for at least 10 elements. This ought help it produce diagnostics, and perhaps help code generation.