Skip to content
Snippets Groups Projects
main.cpp 1.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pelotrio's avatar
    Pelotrio committed
    #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;
    }