Which object declarations in C cause storage to be reserved (i.e. are definitions)?

*爱你&永不变心* 提交于 2019-12-17 16:29:42

问题


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;
[...]

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.


回答1:


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.

  • If you add an initializer to a declaration, it's always a definition:

    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;
    

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

    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.

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

    extern int x;     // <- not a definition
    

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



来源:https://stackoverflow.com/questions/45694056/which-object-declarations-in-c-cause-storage-to-be-reserved-i-e-are-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!