【问题标题】:Difference between variable declaration and definition in C. When does the memory gets allocated? [duplicate]C中变量声明和定义之间的区别。内存何时分配? [复制]
【发布时间】:2020-10-23 07:12:25
【问题描述】:

所以我一直在尝试自学 C。所以只有几个视频和文章以及我身边的一本书。虽然这听起来像是一个简单的概念(我敢肯定),但我认为这个概念对我来说并不清晰。

您能否引用一个变量声明定义的例子?(一起和分开)

就像我读到的一些文章或论坛中所说的那样

int x; (已声明 x)

写在某处

int x; (定义了 x)。

内存何时分配给变量? 又在某处说必须首先定义变量才能分配内存,而在某处说它是在声明变量时分配的?

【问题讨论】:

  • int x; 始终是一个定义
  • 基本上... declare:告诉编译器变量存在并且具有定义的类型,例如:int foobar;。链接器将找出它存在的哪里define 告诉编译器变量存在here 并具有特定值,例如:int foobar = -42;
  • @pmg 你的两种情况都是定义;一个有一个初始化器(提供一个初始值对存储类或其他什么没有影响)
  • 对,第一个是“暂定定义” :)
  • 论坛上很多人混淆了单词,所以请小心。如果你想要确定性,你需要阅读标准。当涉及到这些问题时,许多 C 类书籍的质量令人怀疑。

标签: c variables memory-management definition variable-declaration


【解决方案1】:

就像我读到的一些文章或论坛中所说的那样 诠释 x; ( x 已声明) 某处写着 诠释 x; ( x 已定义)。

实际上int x;同时声明和定义它,所以两者都是有效但不完整的。

声明向编译器显示变量的类型而不创建它。

extern int x; // <- this is declaration

定义创建对象,如果对象在其定义之前声明必须与声明匹配。

extern int x;
int x;

有效但

extern int x;
double x; 

不是。

【讨论】:

    【解决方案2】:

    我希望这能让您大致了解变量何时被声明、分配、初始化、解除分配和消失(销毁)。

    /* global variable A declared and memory for int is allocated */
    /* memory will only be unallocated at global program exit */
    /* global variable A initialized/defined with value 10 */
    int A = 10;
    
    void test(int D){
        /* function test is called by main with argument test(5) */
        /* local variable D is declared, memory for int is allocated */
        /* and initialized with value 5 */
        
        /* add local variable D to global variable A */
        A = A + D;
        
        /* after this point, local variable D will be deallocated */
        /* and will vanish */
    }
    
    int main(int argc, char *argv[]){
        /* local variable B declared, memory for int allocated */
        /* and initialized with value 20 */
        int B = 20;
        
        {
            /* local scoped variable C declared, memory for int allocated */
            /* and initialized with value 30 */
            int C = 30;
            /* after this point, local scoped variable C will be */
            /* deallocated and will vanish */
        }
        
        /* 5 is a local scoped const of size int, memory will be allocated */
        test(5);
        /* after this point, local scoped const 5 will be deallocated */
        /* and will vanish */
        
        
        /* after this point, local variable B will be deallocated */
        /* and will vanish */
        return A;
    }
    

    【讨论】:

    • 您使程序源代码看起来像是按时间顺序依次处理的,包括函数之外的行。我认为这具有误导性。对全局变量使用表达式“分配”是有问题的。为全局变量设置/准备内存的效果是在加载时完成的,根据链接阶段确定的信息,链接阶段发生在编译之后。 (但是,不是我投反对票。)
    • 全局变量将在 main() 之前声明/分配和初始化。所以使用这个顺序是正确的方法。
    • @paladin 不是 DV 但你完全错了,全局变量将在 main() 之前声明/分配和初始化 这不是编译器的工作方式,分配是错误的术语,你的意思是填充?
    • 你们说的都对,但我想保持简单。内存管理比我的示例中显示的要复杂得多。但我希望这能让 OP 了解它的工作原理。
    • 但我想保持简单,这很好,但是分配/释放很混乱,销毁本地对象的过程称为“堆栈展开”,考虑“堆积”用于分配。
    猜你喜欢
    • 2015-07-09
    • 2019-07-25
    • 2011-05-23
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2014-08-27
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多