C macro distribution

I have a macro for repeating macros, which I use to populate arrays with default values ​​at compile time:

const int array [512] = { MACRO_REPEAT(512, -2) // this repeats -2, 512 times [4] = 10, [5] = 2, ... } 

Macro repetition will expand to MACRO_REPEAT_512, but now I wanted to use other macros as the size of the array, for example:

 #define ARRAY_LENGTH 512 const int array [ARRAY_LENGTH ] = { MACRO_REPEAT(ARRAY_LENGTH , -2) // this repeats -2, 512 times [4] = 10, [5] = 2, ... } 

But it expands to MACRO_REPEAT_ARRAY_LENGTH, does not expand the value of ARRAY_LENGTH before concatenation. Another example would be for multidimensional arrays, which includes more extension levels:

 #define X 512 #define Y 512 const int array [X][Y] = { MACRO_REPEAT(X*Y , -2) // this repeats -2, 512 times [4] = 10, [5] = 2, ... } 

This will expand to MARO_REPEAT_X * Y. So, is there a way to expand these values ​​to a finite numeric value before concatenating it with other macros?

+6
source share
2 answers

You can solve the case of MACRO_REPEAT(ARRAY_LENGTH , -2) by changing the definition of MACRO_REPEAT to use a two-stage extension, i.e. do not use token insertion in MACRO_REPEAT , call another macro that does.

Not that it will only work as expected if ARRAY_LENGTH is defined as a token with a single number, and if there is a macro definition for this particular size.

You cannot handle the more general MACRO_REPEAT(X*Y , -2) case with the standard C preprocessor.

You can use the gcc extension to initialize simple arrays:

 #define MACRO_REPEAT(n, e) [ 0 ... (n)-1 ] = (e), 

But this method cannot be used to process multidimensional arrays such as MACRO_REPEAT(X*Y , -2) .

You can try the following:

 #define MACRO_REPEAT(n, e) [ 0 ... (n)-1 ] = (e), #define X 512 #define Y 512 const int array[X][Y] = { MACRO_REPEAT(X, { MACRO_REPEAT(Y, -2) }) }; 

But using the C preprocessor simply hides the intention. If you decide to rely on gcc extensions, just use them directly.

+1
source

I am not sure if this is considered the “correct” answer, since it does not directly answer the OP question, but this is the recommended solution to the problem. It is also not standard C, as it uses the GCC extension.

In the GNU C compiler (gcc), a number of array elements can be initialized with the same value using the form [FIRST ... LAST] = VALUE . It also apparently allows you to use more than one assigned initializer for an element, so you can initialize a range of elements for the same value, and then initialize the elements contained in this range to different values, something like this:

 #define ARRAY_LENGTH 512 const int array[ARRAY_LENGTH] = { [0 ... ARRAY_LENGTH - 1] = -2, [4] = 10, [5] = 2, /* ... */ }; 
+1
source

All Articles