#include<iostream>
using namespace std;
int main()
{
char arr[40960];
for(auto i =0;i<40960;++i)
arr[i]='c';
std::cout<<sizeof(arr)<<std::endl;
return 0;
}
we’re taught local variables allocate in stack ,so we can’t take up too many byte of stack , but in this case program doesnt crash, does anyone know what magic compiler does?
gcc –version
gcc (GCC) 14.1.1 20240522
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
disassemble code:
0000000000001159 <main>:
1159: 55 push %rbp
115a: 48 89 e5 mov %rsp,%rbp
115d: 48 81 ec 20 a0 00 00 sub $0xa020,%rsp
1164: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
116b: 00 00
116d: 48 89 45 f8 mov %rax,-0x8(%rbp)
1171: 31 c0 xor %eax,%eax
1173: c7 85 ec 5f ff ff 00 movl $0x0,-0xa014(%rbp)
117a: 00 00 00
117d: eb 17 jmp 1196 <main+0x3d>
117f: 8b 85 ec 5f ff ff mov -0xa014(%rbp),%eax
1185: 48 98 cltq
1187: c6 84 05 f0 5f ff ff movb $0x63,-0xa010(%rbp,%rax,1)
118e: 63
118f: 83 85 ec 5f ff ff 01 addl $0x1,-0xa014(%rbp)
1196: 81 bd ec 5f ff ff ff cmpl $0x9fff,-0xa014(%rbp)
119d: 9f 00 00
11a0: 7e dd jle 117f <main+0x26>
11a2: be 00 a0 00 00 mov $0xa000,%esi
11a7: 48 8d 05 92 2e 00 00 lea 0x2e92(%rip),%rax # 4040 <_ZSt4cout@GLIBCXX_3.4>
11ae: 48 89 c7 mov %rax,%rdi
11b1: e8 7a fe ff ff call 1030 <_ZNSolsEm@plt>
11b6: 48 8b 15 03 2e 00 00 mov 0x2e03(%rip),%rdx # 3fc0 <_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GLIBCXX_3.4>
11bd: 48 89 d6 mov %rdx,%rsi
11c0: 48 89 c7 mov %rax,%rdi
11c3: e8 78 fe ff ff call 1040 <_ZNSolsEPFRSoS_E@plt>
11c8: b8 00 00 00 00 mov $0x0,%eax
11cd: 48 8b 55 f8 mov -0x8(%rbp),%rdx
11d1: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx
11d8: 00 00
11da: 74 05 je 11e1 <main+0x88>
11dc: e8 6f fe ff ff call 1050 <__stack_chk_fail@plt>
11e1: c9 leave
11e2: c3 ret
sorry for my bad english
2