Skip to content

Wiki for gcc with designated initializer support

doslab edited this page Sep 12, 2012 · 2 revisions

DESIGNATED INITIALIZER SUPPORT FOR G++ COMPILER

The main objective of the project is to compile the linux kernel using a reliable C++ compiler. Our goal was to solve the designated initializer(both trivial and non-trivial) incompatibility between C99 standard and C++ standard, which is recognized by GCC but is not a part of the g++ compiler. Designated initializers have been used in the linux kernel since its an essential component of the GNU C. Hence the problem arises when g++ compiler is used to build the kernel. Since the kernel and gcc are both revised periodically it is a non-trivial task of porting the changes made in the previous compiler to a more recent one. Changes may include general optimizer improvements, language specific improvements, run time library changes and target specific improvements. C99 introduced several new features, many of which had already been implemented as extensions in several compilers.

Parts of the C99 standard are included in the current version of the C++ standard, C++11, including integer types, header files, and library functions. Variable-length arrays are not among these included parts because C++'s Standard Template Library already includes similar functionality. Designated initializers is one of the features present in C99 standard but not in C++ standard. Designated Initializers in C99

Designated initializers are used for the initialization of arrays and structure variables. The initialization for structures can further be divide in two sub-categories.

  1. Trivial : Structure members are initialized in the order they are declared inside structure.
  2. Non-Trivial : Structure members can be initialized in any order irrespective of the order in which they are declared inside structure.

Eg. of designated initializers for arrays Specific index initialization To specify an array index, use `[index] =value ' int a[6] = { [4] = 29, [2] = 15 }; is equivalent to int a[6] = { 0, 0, 15, 0, 29, 0 };

Array range initialization: (This is only a GNU extension) To initialize a range of elements to the same value, use `[first ... last] = value'. For example, int widths[150] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 }; If the value in it has side-effects, the side-effects will happen only once, not for each initialized field by the range initializer.

Mixed array initialization and out of order initialization Each initializer element that does not have a designator applies to the next consecutive element of the array or structure. For example, int a[6] = { [1] = v1, v2, [4] = v4 }; is equivalent to int a[6] = { 0, v1, v2, 0, v4, 0 };

Note: If array length is not specified for designated initializers than the length of the array is the highest index value in initializer plus one.The index values must be constant expressions, even if the array being initialized is automatic.

Eg. of designated initializers for structures Structure field initialization: In a structure initializer, specify the name of a field to initialize with `.fieldname =' before the element value. For example, given the following structure, struct point { int x, y; }; the following initialization struct point p = { .y = yvalue, .x = xvalue }; is equivalent to struct point p = { xvalue, yvalue };

Clone this wiki locally