c++ 函数未设置返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

bool func(int i)
{
if (i == 1)
{
return false;
}
}

int main()
{
bool res;
printf("before: 0x%x\n", res);
res = func(2);
if (res == true)
{
std::cout << "true" << std::endl;
}
if (res == false)
{
std::cout << "false" << std::endl;
}
if (res)
{
std::cout << "Condition is true" << std::endl;
}
if (!res)
{
std::cout << "Condition is false" << std::endl;
}
printf("after: 0x%x\n", res);
}

以一个demo开始,很明显这个程序是错的,因为func函数未设置返回值,编译时也出现警告。

那么程序将输出什么呢?这才是令人奇怪的地方,我认为不设置返回值,不是false就是true,应该没有其他情况。而实际的程序输出如下:

运行几次的after结果都不一样,代表res是一个随机的值
以简化的c++代码生成汇编

1
2
3
4
5
6
7
8
9
10
11
12
bool func(int i)
{
if (i == 1)
{
return false;
}
}

int main()
{
bool res = func(2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	.file	"sample.cpp"
.text
.globl _Z4funci
.type _Z4funci, @function
_Z4funci:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
cmpl $1, -4(%rbp)
jne .L2
movl $0, %eax
jmp .L1
.L2:
.L1:
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size _Z4funci, .-_Z4funci
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $2, %edi
call _Z4funci
movb %al, -1(%rbp)
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:

参数 返回值与寄存器的对应关系如下

截自《深入了解计算机系统》3.4访问信息
可以看出eax寄存器存储返回值,edi为第一个参数

1
2
3
4
5
6
7
8
9
10
11
	movl	%edi, -4(%rbp)
cmpl $1, -4(%rbp) // 将参数与1对比
jne .L2
movl $0, %eax // 相等则将返回值置为0
jmp .L1
.L2:
.L1:
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc

所以说当函数返回值未设置时,eax寄存器的值是随机的,故bool值的变量出现了0xc的值。