传递给函数参数并由同一函数返回的 C99 复合文字
C99 compound literal passed to function parameter and returned by the same function
我想在 C99 中将 uuid 转换为十六进制字符串,并将其传递给在后台使用 printf 格式的日志函数。我想避免单独分配局部变量,因为如果日志被禁用,那么预处理器将删除函数调用并且变量变为未使用,因此会发出警告。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
typedef struct {
uint8_t data[8];
} uuid_64_t;
#define UID_64_STR_MAX_SIZE 24
#define UUID_64_TO_STRING(uuid_64, separator, uppercase) \
uuid_64_to_string((char[UID_64_STR_MAX_SIZE]){ 0 }, \
sizeof((char[UID_64_STR_MAX_SIZE]){ 0 }), \
uuid_64, \
separator, \
uppercase)
const char *bytes_to_hex(char *buffer,
uint32_t buffer_size,
const uint8_t *bytes,
uint32_t bytes_size,
char separator,
bool uppercase)
{
const char hex_char_uppercase[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
const char hex_char_lowercase[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
const char *hex_char = uppercase ? hex_char_uppercase : hex_char_lowercase;
uint32_t total_size, total_separator_size;
// If the separator is set the null character then no separator is used so
// the multiplication by zero results in zero total separator size.
// There is a separator after each two hex characters except for the last two.
total_separator_size = (bytes_size - 1) * (separator != '[=10=]');
// One character shall be reserved for the terminating null character
total_size = 2 * bytes_size + total_separator_size + 1;
if ((buffer == NULL) || (bytes == NULL) || (buffer_size < total_size)) {
return "INVALID";
}
uint32_t out_idx = 0;
for (uint32_t in_idx = 0; in_idx < bytes_size; in_idx++) {
buffer[out_idx++] = hex_char[(bytes[in_idx] >> 4) & 0xF];
buffer[out_idx++] = hex_char[(bytes[in_idx] >> 0) & 0xF];
if (separator != '[=10=]' && (in_idx + 1) < bytes_size) {
buffer[out_idx++] = separator;
}
}
buffer[out_idx] = '[=10=]';
return buffer;
}
const char *uuid_64_to_string(char *buffer,
uint32_t buffer_size,
const uuid_64_t *uuid_64,
char separator,
bool uppercase)
{
return bytes_to_hex(buffer,
buffer_size,
uuid_64->data,
sizeof(uuid_64->data),
separator,
uppercase);
}
int main(void)
{
printf("uuid=%s\r\n", UUID_64_TO_STRING(&uuid_64, ':', true));
}
这个想法是通过一个宏来调用一个函数,该宏将复合文字作为缓冲区参数传递。复合文字分配一个局部变量,其中 uuid_64_to_string 函数通过 bytes_to_hex 函数写入十六进制字符。 uuid_64_to_string returns 这传递了复合文字,以便在类似 printf 的日志调用中直接使用它。
唯一的问题可能是复合文字的生命周期,我对此有点不确定。根据 C99 标准:
The value of the compound literal is that of an unnamed object
initialized by the initializer list. If the compound literal occurs
outside the body of a function, the object has static storage
duration; otherwise, it has automatic storage duration associated with
the enclosing block
因此,在我解释标准时,这应该是定义明确的行为,因为 printf 调用和 uuid_64_to_string 调用在同一个块中。
你怎么看?
宏 UUID_64_TO_STRING
扩展为对 uuid_64_to_string
的函数调用,将指针传递给复合文字 (char[UID_64_STR_MAX_SIZE]){ 0 }
,其范围是 main()
函数中的封闭块。
函数 uuid_64_to_string
return 是它的第一个参数,因此是指向本地数组的指针。可以将其作为参数传递给 printf
,因为它指向的对象是一个 C 字符串,并且具有覆盖 printf
调用执行的生命周期。
相反,将此指针 return 指向调用函数或将其存储到当前作用域外使用的指针中将是错误的:
int main() {
printf("uuid=%s\r\n", UUID_64_TO_STRING(&uuid_64, ':', true)); // OK
return 0;
}
此用途无效:
char *hexify(uuid_64_t *id) {
return UUID_64_TO_STRING(id, ':', true); // NOT OK
}
int main() {
printf("uuid=%s\r\n", hexify(&uuid_64)); // NOT OK
return 0;
}
请注意,范围问题可能很微妙:
int main() {
const char *p = "invalid id";
if (isValidID(uuid_64))
p = UUID_64_TO_STRING(&uuid_64, ':', true);
printf("uuid=%s\r\n", p); // OK
return 0;
}
int main() {
const char *p = "invalid id";
if (isValidID(uuid_64)) {
p = UUID_64_TO_STRING(&uuid_64, ':', true);
}
printf("uuid=%s\r\n", p); // NOT OK
return 0;
}
虽然这个宏看起来很有用,但应该小心使用,可能只用作函数参数。
我想在 C99 中将 uuid 转换为十六进制字符串,并将其传递给在后台使用 printf 格式的日志函数。我想避免单独分配局部变量,因为如果日志被禁用,那么预处理器将删除函数调用并且变量变为未使用,因此会发出警告。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
typedef struct {
uint8_t data[8];
} uuid_64_t;
#define UID_64_STR_MAX_SIZE 24
#define UUID_64_TO_STRING(uuid_64, separator, uppercase) \
uuid_64_to_string((char[UID_64_STR_MAX_SIZE]){ 0 }, \
sizeof((char[UID_64_STR_MAX_SIZE]){ 0 }), \
uuid_64, \
separator, \
uppercase)
const char *bytes_to_hex(char *buffer,
uint32_t buffer_size,
const uint8_t *bytes,
uint32_t bytes_size,
char separator,
bool uppercase)
{
const char hex_char_uppercase[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
const char hex_char_lowercase[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
const char *hex_char = uppercase ? hex_char_uppercase : hex_char_lowercase;
uint32_t total_size, total_separator_size;
// If the separator is set the null character then no separator is used so
// the multiplication by zero results in zero total separator size.
// There is a separator after each two hex characters except for the last two.
total_separator_size = (bytes_size - 1) * (separator != '[=10=]');
// One character shall be reserved for the terminating null character
total_size = 2 * bytes_size + total_separator_size + 1;
if ((buffer == NULL) || (bytes == NULL) || (buffer_size < total_size)) {
return "INVALID";
}
uint32_t out_idx = 0;
for (uint32_t in_idx = 0; in_idx < bytes_size; in_idx++) {
buffer[out_idx++] = hex_char[(bytes[in_idx] >> 4) & 0xF];
buffer[out_idx++] = hex_char[(bytes[in_idx] >> 0) & 0xF];
if (separator != '[=10=]' && (in_idx + 1) < bytes_size) {
buffer[out_idx++] = separator;
}
}
buffer[out_idx] = '[=10=]';
return buffer;
}
const char *uuid_64_to_string(char *buffer,
uint32_t buffer_size,
const uuid_64_t *uuid_64,
char separator,
bool uppercase)
{
return bytes_to_hex(buffer,
buffer_size,
uuid_64->data,
sizeof(uuid_64->data),
separator,
uppercase);
}
int main(void)
{
printf("uuid=%s\r\n", UUID_64_TO_STRING(&uuid_64, ':', true));
}
这个想法是通过一个宏来调用一个函数,该宏将复合文字作为缓冲区参数传递。复合文字分配一个局部变量,其中 uuid_64_to_string 函数通过 bytes_to_hex 函数写入十六进制字符。 uuid_64_to_string returns 这传递了复合文字,以便在类似 printf 的日志调用中直接使用它。 唯一的问题可能是复合文字的生命周期,我对此有点不确定。根据 C99 标准:
The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block
因此,在我解释标准时,这应该是定义明确的行为,因为 printf 调用和 uuid_64_to_string 调用在同一个块中。 你怎么看?
宏 UUID_64_TO_STRING
扩展为对 uuid_64_to_string
的函数调用,将指针传递给复合文字 (char[UID_64_STR_MAX_SIZE]){ 0 }
,其范围是 main()
函数中的封闭块。
函数 uuid_64_to_string
return 是它的第一个参数,因此是指向本地数组的指针。可以将其作为参数传递给 printf
,因为它指向的对象是一个 C 字符串,并且具有覆盖 printf
调用执行的生命周期。
相反,将此指针 return 指向调用函数或将其存储到当前作用域外使用的指针中将是错误的:
int main() {
printf("uuid=%s\r\n", UUID_64_TO_STRING(&uuid_64, ':', true)); // OK
return 0;
}
此用途无效:
char *hexify(uuid_64_t *id) {
return UUID_64_TO_STRING(id, ':', true); // NOT OK
}
int main() {
printf("uuid=%s\r\n", hexify(&uuid_64)); // NOT OK
return 0;
}
请注意,范围问题可能很微妙:
int main() {
const char *p = "invalid id";
if (isValidID(uuid_64))
p = UUID_64_TO_STRING(&uuid_64, ':', true);
printf("uuid=%s\r\n", p); // OK
return 0;
}
int main() {
const char *p = "invalid id";
if (isValidID(uuid_64)) {
p = UUID_64_TO_STRING(&uuid_64, ':', true);
}
printf("uuid=%s\r\n", p); // NOT OK
return 0;
}
虽然这个宏看起来很有用,但应该小心使用,可能只用作函数参数。