Probably a naïve question - I used to program 20 years ago and haven't coded much since. My memory of how the C preprocessor
works has atrophied significantly since then...
可能是一个天真的问题 - 我曾经在20年前制定过程,并且自那时起就没有多少编码。我对C预处理器如何工作的记忆自那时起已经萎缩了......
I am writing a very simple C
program and I am trying to declare a few static global arrays, but the size of the arrays
would be dependent (on a non-trivial way) on a MODE
variable. Something like the simplified example below.
我正在编写一个非常简单的C程序,我试图声明一些静态全局数组,但是数组的大小将依赖于(在非平凡的方式上)MODE变量。类似下面的简化示例。
Two quick points: I know I could just size the arrays
according to the largest size needed by any MODE
, but I don't want to that because (unlike in the simplified example below) sometimes a handful of these dimensions are going to be extremely large while others are tiny.
两个快点:我知道我可以根据任何MODE所需的最大尺寸来调整阵列的大小,但我不想这样做,因为(与下面的简化示例不同)有时候这些维度中的一小部分会非常很大,而其他人很小。
Also, I want to use statically defined global arrays - rather than dynamically allocate them at runtime. I want the compiler to have the sizes at compile time.
此外,我想使用静态定义的全局数组 - 而不是在运行时动态分配它们。我希望编译器在编译时具有大小。
//** Simplified example of what I'd like to do **//
#define SIZE_LIST_1[5] = {2, 7, 23, 33, 12, 76} // I don't think this is valid syntax
#define SIZE_LIST_2[5] = {11, 65, 222, 112, 444}
#define MODE 4
#define S1 SIZE_LIST_1[MODE]
#define S2 SIZE_LIST_2[MODE]
int a[S1], b[S2];
3 个解决方案
#1
10
You need to define a bunch of helper macros first before you can do this in a simple way:
在以简单的方式执行此操作之前,您需要首先定义一堆辅助宏:
#define CONCAT(A,B) A ## B
#define EXPAND_CONCAT(A,B) CONCAT(A, B)
#define ARGN(N, LIST) EXPAND_CONCAT(ARG_, N) LIST
#define ARG_0(A0, ...) A0
#define ARG_1(A0, A1, ...) A1
#define ARG_2(A0, A1, A2, ...) A2
#define ARG_3(A0, A1, A2, A3, ...) A3
#define ARG_4(A0, A1, A2, A3, A4, ...) A4
#define ARG_5(A0, A1, A2, A3, A4, A5, ...) A5
#define ARG_6(A0, A1, A2, A3, A4, A5, A6, ...) A6
#define ARG_7(A0, A1, A2, A3, A4, A5, A6, A7, ...) A7
#define ARG_8(A0, A1, A2, A3, A4, A5, A6, A7, A8, ...) A8
#define ARG_9(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, ...) A9
#define ARG_10(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ...) A10
/* above should be in a pp_helper.h header file or some such */
#define SIZE_LIST_1 ( 2, 7, 23, 33, 12, 76)
#define SIZE_LIST_2 (11, 65, 222, 112, 444, 1000)
#define S1 ARGN(MODE, SIZE_LIST_1)
#define S2 ARGN(MODE, SIZE_LIST_2)
#define MODE 4
int a[S1], b[S2];
There are a bunch of preprocessor 'libraries' you can get with the boilerplate code (boost PP, P99), or you can just roll your own. The main problem being that you need to define ARG macros based on the largest number of arguments you'll ever want to handle.
你可以通过样板代码(boost PP,P99)获得一堆预处理器“库”,或者你可以自己动手。主要问题是您需要根据您想要处理的最大数量的参数来定义ARG宏。
#2
7
Probably the best you can do is something like this:
你可以做的最好的事情可能是这样的:
#define SIZE_LIST_1_0 2
#define SIZE_LIST_1_1 7
#define SIZE_LIST_1_2 23
#define SIZE_LIST_1_3 33
#define SIZE_LIST_1_4 12
#define SIZE_LIST_2_0 11
#define SIZE_LIST_2_1 65
#define SIZE_LIST_2_2 222
#define SIZE_LIST_2_3 112
#define SIZE_LIST_2_4 444
#define MODE 4
#define S1 SIZE_LIST_1_##MODE
#define S2 SIZE_LIST_2_##MODE
int a[S1], b[S2];
#3
3
I'm afraid there is no such possibility.
我担心没有这种可能性。
I suggest the following approach instead:
我建议采用以下方法:
#define MODE 0
#define DECLARE_ARRAYS_WITH_SIZES(S1, S2, S3) \
int arr1[S1]; \
int arr2[S2]; \
int arr3[S3];
#if MODE == 0
DECLARE_ARRAYS_WITH_SIZES(3, 6, 7)
#elif MODE == 1
DECLARE_ARRAYS_WITH_SIZES(8, 2, 1)
#endif
#1
10
You need to define a bunch of helper macros first before you can do this in a simple way:
在以简单的方式执行此操作之前,您需要首先定义一堆辅助宏:
#define CONCAT(A,B) A ## B
#define EXPAND_CONCAT(A,B) CONCAT(A, B)
#define ARGN(N, LIST) EXPAND_CONCAT(ARG_, N) LIST
#define ARG_0(A0, ...) A0
#define ARG_1(A0, A1, ...) A1
#define ARG_2(A0, A1, A2, ...) A2
#define ARG_3(A0, A1, A2, A3, ...) A3
#define ARG_4(A0, A1, A2, A3, A4, ...) A4
#define ARG_5(A0, A1, A2, A3, A4, A5, ...) A5
#define ARG_6(A0, A1, A2, A3, A4, A5, A6, ...) A6
#define ARG_7(A0, A1, A2, A3, A4, A5, A6, A7, ...) A7
#define ARG_8(A0, A1, A2, A3, A4, A5, A6, A7, A8, ...) A8
#define ARG_9(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, ...) A9
#define ARG_10(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ...) A10
/* above should be in a pp_helper.h header file or some such */
#define SIZE_LIST_1 ( 2, 7, 23, 33, 12, 76)
#define SIZE_LIST_2 (11, 65, 222, 112, 444, 1000)
#define S1 ARGN(MODE, SIZE_LIST_1)
#define S2 ARGN(MODE, SIZE_LIST_2)
#define MODE 4
int a[S1], b[S2];
There are a bunch of preprocessor 'libraries' you can get with the boilerplate code (boost PP, P99), or you can just roll your own. The main problem being that you need to define ARG macros based on the largest number of arguments you'll ever want to handle.
你可以通过样板代码(boost PP,P99)获得一堆预处理器“库”,或者你可以自己动手。主要问题是您需要根据您想要处理的最大数量的参数来定义ARG宏。
#2
7
Probably the best you can do is something like this:
你可以做的最好的事情可能是这样的:
#define SIZE_LIST_1_0 2
#define SIZE_LIST_1_1 7
#define SIZE_LIST_1_2 23
#define SIZE_LIST_1_3 33
#define SIZE_LIST_1_4 12
#define SIZE_LIST_2_0 11
#define SIZE_LIST_2_1 65
#define SIZE_LIST_2_2 222
#define SIZE_LIST_2_3 112
#define SIZE_LIST_2_4 444
#define MODE 4
#define S1 SIZE_LIST_1_##MODE
#define S2 SIZE_LIST_2_##MODE
int a[S1], b[S2];
#3
3
I'm afraid there is no such possibility.
我担心没有这种可能性。
I suggest the following approach instead:
我建议采用以下方法:
#define MODE 0
#define DECLARE_ARRAYS_WITH_SIZES(S1, S2, S3) \
int arr1[S1]; \
int arr2[S2]; \
int arr3[S3];
#if MODE == 0
DECLARE_ARRAYS_WITH_SIZES(3, 6, 7)
#elif MODE == 1
DECLARE_ARRAYS_WITH_SIZES(8, 2, 1)
#endif