zoukankan      html  css  js  c++  java
  • 浅析C语言定义时赋值、定义后赋值、定义时不赋值

    引言

    其实在我看来定义一个变量后再赋值和定义的时候直接初始化应该是一个意思,现在就来验证一下,之前貌似验证过是一致的,但是我需要留下证据。

    code

    类型 内容
    定义时赋值 #include <stdio.h> int main(){ int a = 10; return 0;}
    定义后赋值 #include <stdio.h> int main(){ int a; a = 10; return 0;}
    定义不赋值 #include <stdio.h> int main(){ int a; return 0;}

    预编译

    gcc -E -I./inc *.c -o *.i

    这里都是一样的,由于比较长就不展示了。*表示对应的各个文件,这里用这个符号表示而已。

    编译

    gcc -S -I./inc *.c -o *.s

    定义时赋值

    	.file	"yubian.c"
    	.text
    	.globl	main
    	.type	main, @function
    main:
    .LFB0:
    	.cfi_startproc
    	pushq	%rbp //将调用这个函数的函数的栈帧的底部栈指针存进本函数栈帧。用于恢复调用本函数的函数。
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp //将栈顶指针数据赋值给栈底指针。这时进入到了新的函数栈帧。
    	.cfi_def_cfa_register 6
    	movl	$10, -4(%rbp) // 这里给int c 开辟栈空间  并赋值10
    	movl	$0, %eax	//返回值赋值0
    	popq	%rbp	//弹出数据,放入基址寄存器中。也就是本栈帧中存储的第一个数据:调用函数的栈底指针函数,用来返回,调用函数。
    	.cfi_def_cfa 7, 8
    	ret
    	.cfi_endproc
    .LFE0:
    	.size	main, .-main
    	.ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
    	.section	.note.GNU-stack,"",@progbits
    
    

    定义后赋值

    	.file	"yubianyi.c"
    	.text
    	.globl	main
    	.type	main, @function
    main:
    .LFB0:
    	.cfi_startproc
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp
    	.cfi_def_cfa_register 6
    	movl	$10, -4(%rbp)
    	movl	$0, %eax
    	popq	%rbp
    	.cfi_def_cfa 7, 8
    	ret
    	.cfi_endproc
    .LFE0:
    	.size	main, .-main
    	.ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
    	.section	.note.GNU-stack,"",@progbits
    
    

    定义不赋值

    	.file	"yu.c"
    	.text
    	.globl	main
    	.type	main, @function
    main:
    .LFB0:
    	.cfi_startproc
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp
    	.cfi_def_cfa_register 6
    	movl	$0, %eax
    	popq	%rbp
    	.cfi_def_cfa 7, 8
    	ret
    	.cfi_endproc
    .LFE0:
    	.size	main, .-main
    	.ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
    	.section	.note.GNU-stack,"",@progbits
    
    

    定义直接赋值和定以后马上赋值在这里还是一样的。而不赋值的时候,少了一条movl $10, -4(%rbp),这句话的含义就是这里给int c 开辟栈空间 并赋值10。汇编以了解为主,目前不做深入。

    汇编

    汇编指令

    gcc -c *.s -o *.o

    查看指令

    nm *.o

    wzh@ubuntu:~/cipan/ctest$ nm *.o
    
    yubian.o:
    0000000000000000 T main
    
    yubianyi.o:
    0000000000000000 T main
    
    yu.o:
    0000000000000000 T main
    
    

    查看指令

    readelf -a yubianyi.o yubian.o yu.o

    wzh@ubuntu:~/cipan/ctest$ readelf -a yubianyi.o yubian.o yu.o
    
    File: yubianyi.o
    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              REL (Relocatable file)
      Machine:                           Advanced Micro Devices X86-64
      Version:                           0x1
      Entry point address:               0x0
      Start of program headers:          0 (bytes into file)
      Start of section headers:          544 (bytes into file)
      Flags:                             0x0
      Size of this header:               64 (bytes)
      Size of program headers:           0 (bytes)
      Number of program headers:         0
      Size of section headers:           64 (bytes)
      Number of section headers:         11
      Section header string table index: 8
    
    Section Headers:
      [Nr] Name              Type             Address           Offset
           Size              EntSize          Flags  Link  Info  Align
      [ 0]                   NULL             0000000000000000  00000000
           0000000000000000  0000000000000000           0     0     0
      [ 1] .text             PROGBITS         0000000000000000  00000040
           0000000000000012  0000000000000000  AX       0     0     1
      [ 2] .data             PROGBITS         0000000000000000  00000052
           0000000000000000  0000000000000000  WA       0     0     1
      [ 3] .bss              NOBITS           0000000000000000  00000052
           0000000000000000  0000000000000000  WA       0     0     1
      [ 4] .comment          PROGBITS         0000000000000000  00000052
           0000000000000036  0000000000000001  MS       0     0     1
      [ 5] .note.GNU-stack   PROGBITS         0000000000000000  00000088
           0000000000000000  0000000000000000           0     0     1
      [ 6] .eh_frame         PROGBITS         0000000000000000  00000088
           0000000000000038  0000000000000000   A       0     0     8
      [ 7] .rela.eh_frame    RELA             0000000000000000  000001b0
           0000000000000018  0000000000000018   I       9     6     8
      [ 8] .shstrtab         STRTAB           0000000000000000  000001c8
           0000000000000054  0000000000000000           0     0     1
      [ 9] .symtab           SYMTAB           0000000000000000  000000c0
           00000000000000d8  0000000000000018          10     8     8
      [10] .strtab           STRTAB           0000000000000000  00000198
           0000000000000011  0000000000000000           0     0     1
    Key to Flags:
      W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
      I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
      O (extra OS processing required) o (OS specific), p (processor specific)
    
    There are no section groups in this file.
    
    There are no program headers in this file.
    
    Relocation section '.rela.eh_frame' at offset 0x1b0 contains 1 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
    
    The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
    
    Symbol table '.symtab' contains 9 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS yubianyi.c
         2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
         3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
         4: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
         5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
         6: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
         7: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
         8: 0000000000000000    18 FUNC    GLOBAL DEFAULT    1 main
    
    No version information found in this file.
    
    File: yubian.o
    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              REL (Relocatable file)
      Machine:                           Advanced Micro Devices X86-64
      Version:                           0x1
      Entry point address:               0x0
      Start of program headers:          0 (bytes into file)
      Start of section headers:          536 (bytes into file)
      Flags:                             0x0
      Size of this header:               64 (bytes)
      Size of program headers:           0 (bytes)
      Number of program headers:         0
      Size of section headers:           64 (bytes)
      Number of section headers:         11
      Section header string table index: 8
    
    Section Headers:
      [Nr] Name              Type             Address           Offset
           Size              EntSize          Flags  Link  Info  Align
      [ 0]                   NULL             0000000000000000  00000000
           0000000000000000  0000000000000000           0     0     0
      [ 1] .text             PROGBITS         0000000000000000  00000040
           0000000000000012  0000000000000000  AX       0     0     1
      [ 2] .data             PROGBITS         0000000000000000  00000052
           0000000000000000  0000000000000000  WA       0     0     1
      [ 3] .bss              NOBITS           0000000000000000  00000052
           0000000000000000  0000000000000000  WA       0     0     1
      [ 4] .comment          PROGBITS         0000000000000000  00000052
           0000000000000036  0000000000000001  MS       0     0     1
      [ 5] .note.GNU-stack   PROGBITS         0000000000000000  00000088
           0000000000000000  0000000000000000           0     0     1
      [ 6] .eh_frame         PROGBITS         0000000000000000  00000088
           0000000000000038  0000000000000000   A       0     0     8
      [ 7] .rela.eh_frame    RELA             0000000000000000  000001a8
           0000000000000018  0000000000000018   I       9     6     8
      [ 8] .shstrtab         STRTAB           0000000000000000  000001c0
           0000000000000054  0000000000000000           0     0     1
      [ 9] .symtab           SYMTAB           0000000000000000  000000c0
           00000000000000d8  0000000000000018          10     8     8
      [10] .strtab           STRTAB           0000000000000000  00000198
           000000000000000f  0000000000000000           0     0     1
    Key to Flags:
      W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
      I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
      O (extra OS processing required) o (OS specific), p (processor specific)
    
    There are no section groups in this file.
    
    There are no program headers in this file.
    
    Relocation section '.rela.eh_frame' at offset 0x1a8 contains 1 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
    
    The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
    
    Symbol table '.symtab' contains 9 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS yubian.c
         2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
         3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
         4: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
         5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
         6: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
         7: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
         8: 0000000000000000    18 FUNC    GLOBAL DEFAULT    1 main
    
    No version information found in this file.
    
    File: yu.o
    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              REL (Relocatable file)
      Machine:                           Advanced Micro Devices X86-64
      Version:                           0x1
      Entry point address:               0x0
      Start of program headers:          0 (bytes into file)
      Start of section headers:          536 (bytes into file)
      Flags:                             0x0
      Size of this header:               64 (bytes)
      Size of program headers:           0 (bytes)
      Number of program headers:         0
      Size of section headers:           64 (bytes)
      Number of section headers:         11
      Section header string table index: 8
    
    Section Headers:
      [Nr] Name              Type             Address           Offset
           Size              EntSize          Flags  Link  Info  Align
      [ 0]                   NULL             0000000000000000  00000000
           0000000000000000  0000000000000000           0     0     0
      [ 1] .text             PROGBITS         0000000000000000  00000040
           000000000000000b  0000000000000000  AX       0     0     1
      [ 2] .data             PROGBITS         0000000000000000  0000004b
           0000000000000000  0000000000000000  WA       0     0     1
      [ 3] .bss              NOBITS           0000000000000000  0000004b
           0000000000000000  0000000000000000  WA       0     0     1
      [ 4] .comment          PROGBITS         0000000000000000  0000004b
           0000000000000036  0000000000000001  MS       0     0     1
      [ 5] .note.GNU-stack   PROGBITS         0000000000000000  00000081
           0000000000000000  0000000000000000           0     0     1
      [ 6] .eh_frame         PROGBITS         0000000000000000  00000088
           0000000000000038  0000000000000000   A       0     0     8
      [ 7] .rela.eh_frame    RELA             0000000000000000  000001a8
           0000000000000018  0000000000000018   I       9     6     8
      [ 8] .shstrtab         STRTAB           0000000000000000  000001c0
           0000000000000054  0000000000000000           0     0     1
      [ 9] .symtab           SYMTAB           0000000000000000  000000c0
           00000000000000d8  0000000000000018          10     8     8
      [10] .strtab           STRTAB           0000000000000000  00000198
           000000000000000b  0000000000000000           0     0     1
    Key to Flags:
      W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
      I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
      O (extra OS processing required) o (OS specific), p (processor specific)
    
    There are no section groups in this file.
    
    There are no program headers in this file.
    
    Relocation section '.rela.eh_frame' at offset 0x1a8 contains 1 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
    
    The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
    
    Symbol table '.symtab' contains 9 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS yu.c
         2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
         3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
         4: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
         5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
         6: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
         7: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
         8: 0000000000000000    11 FUNC    GLOBAL DEFAULT    1 main
    
    No version information found in this file.
    
    

    定义时赋值和定义后赋值,区别就在文件的大小不同,定以后赋值会稍微大一点。其他一致。
    定义时赋值和定义时不赋值,文件大小基本保持一致。
    定义时赋值和定以后赋值的.text相同,而定义后不赋值,执行的语句会少一条,这个会在.text中体现。
    这里算是对编译时的一个详细说明了,其实和编译时产生的不同基本保持一致

    链接

    这里保持一致。

  • 相关阅读:
    XML 的学习笔记3
    XML 的学习笔记2
    XML 的学习笔记1
    Tomcat 学习笔记2
    Tomcat 学习笔记1
    sol
    sol
    0、安装Ionic2
    ionic2 目录
    6、Angular Route 路由
  • 原文地址:https://www.cnblogs.com/still-smile/p/14917372.html
Copyright © 2011-2022 走看看