gpt4 book ai didi

c - 为什么我不能用其他静态常量指针初始化静态常量指针?

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:36 24 4
gpt4 key购买 nike

我不完全确定以下内容是否会破坏 C 标准,但 clang 允许使用另一个 static const scalar 的内容初始化 static const scalar 类型的变量变量,示例:

static const long temp = 10;
static const long temp1 = temp;

当我尝试使用指针时,它失败了。

static const long *const temp = (void *)1000; // ok
static const long *const temp2 = (void *)&temp2; // ok
static const long *const temp1 = temp;
// ^^^^ error: initializer element is not a compile-time constant

克服这个问题的一种方法是简单地使用 long 或任何其他大到足以容纳地址的标量类型......

static unsigned long long temp;
static const unsigned long long addr = (unsigned long long)&temp;
static const unsigned long long addr2 = addr;

这听起来很可怕,更不用说我不完全确定它的安全性,因为我不知道链接器/加载器是否可以/将更新地址......

关于如何甚至是否可以用另一个静态常量指针的内容初始化静态常量指针的任何想法......更准确地说,我正在尝试使用函数指针,但注意到其他指针也有同样的事情类型……

最佳答案

这是涵盖此内容的文字:

C11 6.6/7:

More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following:

  • an arithmetic constant expression,
  • a null pointer constant,
  • an address constant, or
  • an address constant for a complete object type plus or minus an integer constant expression.

C11 6.6/9:

An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. The array-subscript [] and member-access . and -> operators, the address & and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.

C11 6.6/10:

An implementation may accept other forms of constant expressions.

因此,您的 temp 不符合地址常量的条件,因为它不满足地址常量定义中的任何选项。

理由: IDK,这对我来说似乎是一个疏忽。也许是为了避免给编译器带来不必要的负担;例如如果 (void *)1000 实际上没有指向对象(例如,它指向进程地址空间之外,或者是陷阱表示),则评估 temp 会导致未定义的行为(无需诊断)。

解决方法: 使用 unsigned long long 不如使用 uintptr_t。然而,

static const long *const temp1 = (void *)1000; 

似乎是比整数选项更好的选择。您可以使用 #define 宏来避免重复实际地址。

关于c - 为什么我不能用其他静态常量指针初始化静态常量指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25838879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com