C中的哪些对象声明导致保留存储(即定义)? [英] Which object declarations in C cause storage to be reserved (i.e. are definitions)?

查看:96
本文介绍了C中的哪些对象声明导致保留存储(即定义)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C11在6.7节中指定哪些声明也是定义:

C11 specifies in section 6.7 which declarations are also definitions:

标识符的定义是对该标识符的声明,该声明:
—对于一个对象,导致为该对象保留存储空间;
[...]

A definition of an identifier is a declaration for that identifier that:
— for an object, causes storage to be reserved for that object;
[...]

我没有找到导致对象保留的对象声明的完整列表.直观上我很清楚,但是我无法从C11标准中获取此信息.

I didn't find a comprehensive list of which object declarations cause storage to be reserved. Intuitively it is clear to me, but I wasn't able to get this information out of the C11 standard.

推荐答案

没有明确的清单,因为该标准仅描述了什么是定义,而且不在一个地方.我会在这里总结一下.在这里,我将只使用int类型,而没有任何限定符(例如const)以保持一致性.

There's no definitive list because the standard just describes what are definitions and it's not in a single place. I'll try to sum it up here. I'll only use the type int here without any qualifiers (like const) for consistency.

  • 如果在声明中添加初始化程序,则它始终是定义:

int x = 1;

  • 没有初始化程序,当它们在功能范围中时,它们是定义:

  • Without an initializer, the following are definitions when they're in function scope:

    int x;
    auto int x;        // auto is the default anyways
    register int x;    // register is just a hint, but would be "storage" as well
    static int x;      // also reserves storage, but with static duration
    

  • 文件范围中,规则稍微复杂一些;以下是临时定义

  • In file scope, the rules are a bit more complicated; the following are tentative definitions:

    int x;
    static int x;
    

    该标准(第6.9.2p2节)的措辞是:

    The wording of the standard (§6.9.2p2) is:

    具有文件范围而没有初始化程序的对象的标识符声明,以及 没有存储类说明符或具有存储类说明符static的情况下, 临时定义.如果翻译单元包含一个或多个临时定义, 标识符,并且转换单元不包含该标识符的外部定义,然后 该行为与翻译单元中包含的文件作用域声明完全相同 标识符,具有转换单元末尾的复合类型,并带有初始化程序 等于0

    A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0

    因此,这意味着如果未找到它们引用另一个定义,它们最终将成为定义".

    so this means that they eventually "become definitions" if they are not found to refer to another definition.

    使用存储类extern并且没有初始化程序,您就没有定义:

    With the storage class extern and without an initializer, you don't have a definition:

    extern int x;     // <- not a definition
    

  • AFAIK,这应该是完整的规则集.如果我忘记了某些内容,请随时进行编辑/评论.

    AFAIK, this should be the complete set of rules. Please feel free to edit/comment if I forgot something.

    这篇关于C中的哪些对象声明导致保留存储(即定义)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆