文字数组或化合物的使用
Usage of Literal arrays or compounds
我有一个结构
typedef struct
{
int num_bytes; // number of relevant bytes
void * bytes_p; // pointer to bytes
} BYTES_T;
在很多地方作为参数传递给很多函数。
通常我是这样使用的:
uint8_t my_array[] = {0x01, 0x03, 0xff} ; // example array
BYTES_T my_bytes;
my_bytes.bytes_p = my_array;
my_bytes.num_bytes= sizeof(my_array);
...
foo(&my_bytes); // call a function
为了使我的部分代码更具可读性,我想避免在同一上下文中同时创建数组和结构。
因此,作为第一次尝试,我使用复合文字尝试了一个宏:
#define ARRAYINFO(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof(((A)))})
并像
一样使用它
foo(ARRAYINFO(my_array));
这有效 - 当传递一个数组时。但是当我将指针传递给一个数组时,它当然会产生废话:
uint8_t * array_pointer = my_array; // this is allowed
foo(ARRAYINFO(array_pointer )); // nonsense for num_bytes because sizeof()
因此,这是危险的,不是一种选择。
接下来我想了一个构造来创建结构而不显式创建数组:
#define CREATE_BYTES(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof((A))})
BYTES_T my_bytes = CREATE_BYTES(((uint8_t []){1,2,3,4,5}));
这似乎可行,但我认为表示传递的文字的 "internal" 数组在内存中创建了两次(宏对其求值两次)。
这样的想法有解决方案吗?
This seems to work, but I think that the "internal" array ,
representing the passed literal is created twice in memory (the macro
evaluates it twice).
来自 C99 标准:
6.5.3.4/2
The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is an integer constant.
因此 A
在您的宏中只计算一次:
#define CREATE_BYTES(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof((A))})
我有一个结构
typedef struct
{
int num_bytes; // number of relevant bytes
void * bytes_p; // pointer to bytes
} BYTES_T;
在很多地方作为参数传递给很多函数。
通常我是这样使用的:
uint8_t my_array[] = {0x01, 0x03, 0xff} ; // example array
BYTES_T my_bytes;
my_bytes.bytes_p = my_array;
my_bytes.num_bytes= sizeof(my_array);
...
foo(&my_bytes); // call a function
为了使我的部分代码更具可读性,我想避免在同一上下文中同时创建数组和结构。
因此,作为第一次尝试,我使用复合文字尝试了一个宏:
#define ARRAYINFO(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof(((A)))})
并像
一样使用它foo(ARRAYINFO(my_array));
这有效 - 当传递一个数组时。但是当我将指针传递给一个数组时,它当然会产生废话:
uint8_t * array_pointer = my_array; // this is allowed
foo(ARRAYINFO(array_pointer )); // nonsense for num_bytes because sizeof()
因此,这是危险的,不是一种选择。
接下来我想了一个构造来创建结构而不显式创建数组:
#define CREATE_BYTES(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof((A))})
BYTES_T my_bytes = CREATE_BYTES(((uint8_t []){1,2,3,4,5}));
这似乎可行,但我认为表示传递的文字的 "internal" 数组在内存中创建了两次(宏对其求值两次)。
这样的想法有解决方案吗?
This seems to work, but I think that the "internal" array , representing the passed literal is created twice in memory (the macro evaluates it twice).
来自 C99 标准:
6.5.3.4/2
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
因此 A
在您的宏中只计算一次:
#define CREATE_BYTES(A) &((BYTES_T ){.bytes_p = ((A)), .num_bytes = sizeof((A))})