Newer
Older
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
#include <iostream>
#include <climits>
int stack_limit(int x, int y, int z, int w) {
// Print the current stack pointer
std::cout << "Stack pointer: " << &x << std::endl;
// Recursively call the function with an incremented parameter
return stack_limit(x + 1, y + 1, z + 1, w + 1);
}
void heapLimit() {
// Try allocating arrays of increasing size until the allocation fails
const unsigned long long increment_size = 1024 * 1024;
// Try allocating arrays of increasing size until the allocation fails
for (unsigned long long i = increment_size; i <= ULLONG_MAX; i += increment_size) {
try {
char *heap_array = new char[i];
std::cout << "Heap limit: " << i << " bytes" << std::endl;
delete[] heap_array;
}
catch (std::bad_alloc &) {
std::cout << "Heap limit reached at " << i - increment_size << " bytes" << std::endl;
// Convert the number of bytes to megabytes
std::cout << "Heap limit reached at " << (i - increment_size) / (1024 * 1024) << " MB" << std::endl;
break;
}
}
}
int main() {
// Get the stack pointer before calling the recursive function
int x = 0;
std::cout << "Stack pointer before: " << &x << std::endl;
// Call the stack_limit function with four large parameters
stack_limit(INT_MAX, INT_MAX, INT_MAX, INT_MAX);
heapLimit();
return 0;
}