c - 是__attribute __((nonnull))是标准C

标签 c compiler-errors

我想知道__attribute__((nonnull))是标准C还是编译器特定的。如果是特定于编译器的,那么是否有其他方法可以对标准C进行相同处理。

在这里,我试图防止静态分析器可能出现的空指针取消引用警告。但是我不想让我的代码编译器依赖。

最佳答案

它是特定于编译器的。在C11 standard的任何地方都没有提及属性和nonnull。

在C11中,可以使用type ParameterName[static 1]语法,但是如果您传递NULL参数,则只有clang和zapcc(在gcc <= 7.1和clang> = 3.1,zapcc和icc中)会生成警告。
(不幸的是,它不能与空指针一起使用)。

__attribute__((__nonnull__)) /*nonstandard*/
void pass_nonnull0(char *X)
{
}

void pass_nonnull1(char X[static 1]) /*standard*/
{ /*the "static 1" means the pointed-to "array" must have at least 1 element*/
}

int main()
{
    pass_nonnull0(0); /* both clang & gcc warn with nonnull attributes */
    pass_nonnull1(0); /* only clang and zapcc warn with type ArgName [static 1] */
}

D[ static type-qualifier-listopt assignment-expression ]语法的语义并不能真正保证警告。该语法仅向编译器表示一个 promise ,即指向对象的对象将至少包含N个元素:

6.7.6.3p7:

A declaration of a parameter as ''array of type'' shall be adjusted to ''qualified pointer to type'', where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.



但是,如果编译器可以看到 promise 被破坏,则生成警告是明智的。

关于c - 是__attribute __((nonnull))是标准C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237148/

相关文章:

c - AF_UNIX 套接字开销?

c - 在 autoconf 中探测符号可见性扩展的最佳实践

c++一个变量的多个定义

android - toast消息在android java方法中编译错误

c++ - 初始化静态变量时出现语法错误

c - 重新分配实现

c++ - Linux 中的 C++ 编程资源

c++ - 两个相乘的每个子集的加法

java - 如何比较方法中的两个实例变量?

python - Python类继承查询