fork download
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <limits>
  4.  
  5. void mallocbsearch(std::size_t lower, std::size_t upper)
  6. {
  7. std::cout<<"["<<lower<<", "<<upper<<"]\n";
  8. if(upper-lower==1)
  9. {
  10. std::cout<<"Found! "<<lower<<"\n";
  11. return;
  12. }
  13. std::size_t mid=lower+(upper-lower)/2;
  14. void *ptr=std::malloc(mid);
  15. if(ptr)
  16. {
  17. free(ptr);
  18. mallocbsearch(mid, upper);
  19. }
  20. else
  21. mallocbsearch(lower, mid);
  22. }
  23.  
  24. int main()
  25. {
  26. mallocbsearch(0, std::numeric_limits<std::size_t>::max());
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 4320KB
stdin
Standard input is empty
stdout
[0, 4294967295]
[0, 2147483647]
[0, 1073741823]
[536870911, 1073741823]
[536870911, 805306367]
[536870911, 671088639]
[536870911, 603979775]
[536870911, 570425343]
[553648127, 570425343]
[562036735, 570425343]
[562036735, 566231039]
[562036735, 564133887]
[563085311, 564133887]
[563609599, 564133887]
[563871743, 564133887]
[564002815, 564133887]
[564002815, 564068351]
[564002815, 564035583]
[564002815, 564019199]
[564002815, 564011007]
[564006911, 564011007]
[564008959, 564011007]
[564009983, 564011007]
[564010495, 564011007]
[564010751, 564011007]
[564010879, 564011007]
[564010943, 564011007]
[564010975, 564011007]
[564010991, 564011007]
[564010991, 564010999]
[564010995, 564010999]
[564010995, 564010997]
[564010996, 564010997]
Found! 564010996