淺析C語言定義時賦值、定義後賦值、定義時不賦值
阿新 • • 發佈:2021-06-22
引言
其實在我看來定義一個變數後再賦值和定義的時候直接初始化應該是一個意思,現在就來驗證一下,之前貌似驗證過是一致的,但是我需要留下證據。
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中體現。
這裡算是對編譯時的一個詳細說明了,其實和編譯時產生的不同基本保持一致
連結
這裡保持一致。