声明变量但不初始化它会提高性能吗?

Fut*_*Sci 1 c c++ java initialization declaration

我目前正在学习计算机科学,并希望了解所有内容,所以我在书中注意到人们声明了一个变量int x;然后在声明的正下方初始化它,例如

int x;
x = 0;
Run Code Online (Sandbox Code Playgroud)

我想知道这是否对编译代码的内存或效率有任何积极意义?

Bri*_*ain 7

我目前正在学习计算机科学,并希望了解一切

你来对地方了!

如果这对内存或编译代码的效率有积极作用

它没有.但你怎么知道呢?经验数据,兄弟!

花点时间研究我的编译器中的这两个中间输出.这是一个传说:最左边的列不是很有趣,忽略它.下一列显示源文件名(example2.c)的名称或0000 55从源生成的可执行机器指令().显示原始源的行,您会看到四个星号.它与编译时生成的相应程序集输出交错.指令mnemomics和参数显示在可执行指令的右侧.在两个示例之间来回查看,您可以看到两个示例中的说明相同.

我使用gcc(gcc -c -g -Wa,-ahl=example.s example.c)创建了这些示例.

首先,"理智"初始化:

   6                with_init:                                         
   7                .LFB0:
   8                    .file 1 "example2.c"                           
   1:example2.c    **** 
   2:example2.c    **** int with_init()                                
   3:example2.c    **** {
   9                    .loc 1 3 0
  10                    .cfi_startproc                                 
  11 0000 55            pushq   %rbp                                   
  12                .LCFI0:
  13                    .cfi_def_cfa_offset 16                         
  14                    .cfi_offset 6, -16                             
  15 0001 4889E5        movq    %rsp, %rbp                             
  16                .LCFI1:
  17                    .cfi_def_cfa_register 6                        
   4:example2.c    ****     int x = 0;                                 
  18                    .loc 1 4 0
  19 0004 C745FC00      movl    $0, -4(%rbp)                           
  19      000000   
   5:example2.c    ****     
   6:example2.c    ****     return x;                                  
  20                    .loc 1 6 0
  21 000b 8B45FC        movl    -4(%rbp), %eax                         
   7:example2.c    **** }
  22                    .loc 1 7 0
  23 000e 5D            popq    %rbp                                   
  24                .LCFI2:
  25                    .cfi_def_cfa 7, 8                              
  26 000f C3            ret
Run Code Online (Sandbox Code Playgroud)

现在,你提出了更"有趣"的案例:

   6                later_init:
   7                .LFB0:
   8                    .file 1 "example.c"
   1:example.c     ****
   2:example.c     **** int later_init()
   3:example.c     **** {
   9                    .loc 1 3 0
  10                    .cfi_startproc
  11 0000 55            pushq   %rbp
  12                .LCFI0:
  13                    .cfi_def_cfa_offset 16
  14                    .cfi_offset 6, -16
  15 0001 4889E5        movq    %rsp, %rbp
  16                .LCFI1:
  17                    .cfi_def_cfa_register 6
   4:example.c     ****     int x;
   5:example.c     ****
   6:example.c     ****     x = 0;
  18                    .loc 1 6 0
  19 0004 C745FC00      movl    $0, -4(%rbp)
  19      000000
   7:example.c     ****
   8:example.c     ****     return x;
  20                    .loc 1 8 0
  21 000b 8B45FC        movl    -4(%rbp), %eax
   9:example.c     **** }
  22                    .loc 1 9 0
  23 000e 5D            popq    %rbp
  24                .LCFI2:
  25                    .cfi_def_cfa 7, 8
  26 000f C3            ret
Run Code Online (Sandbox Code Playgroud)

没有不同!

编辑:我之前没有看到java标签.在这种情况下,可以说更直接:

$ cat example.java 

class SOComparison
{

    public static int with_init()
    {
        int x = 0;

        return x;
    }

    public static int later_init()
    {
        int x;

        x = 0;

        return x;
    }

}
$ javap -c SOComparison
Compiled from "example.java"
class SOComparison {
  SOComparison();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public static int with_init();
    Code:
       0: iconst_0      
       1: istore_0      
       2: iload_0       
       3: ireturn       

  public static int later_init();
    Code:
       0: iconst_0      
       1: istore_0      
       2: iload_0       
       3: ireturn       
}
Run Code Online (Sandbox Code Playgroud)