递归调用次数的实验

此处需要上机实验

  1. // Could we do recursion calls as we want? NO, you have stack limit size.
  2. // Say your function call reserve 1 integer (4 bytes) and a call need 2 pointers (8 bytes) for saving some return addresses
  3. // Say you have 1.5MB stack size, then you could do maximum 1.5MB / 12 bytes ~= 144617 call
  4. int dep = 0;
  5. void stackTest1()
  6. {
  7. cout<<dep++<<"\n"<<flush;
  8. stackTest1();
  9. }
  10. void stackTest2(int a = 1)
  11. {
  12. cout<<dep++<<"\n"<<flush;
  13. stackTest2(a);
  14. }
  15. void stackTest3()
  16. {
  17. cout<<dep++<<"\n"<<flush;
  18. int arr[50]; // be careful from your created items. E.g. Integrs, Strings, Structs.
  19. stackTest3();
  20. }
  21. void stackTest4()
  22. {
  23. cout<<dep++<<"\n"<<flush;
  24. int arr[100]; // be careful from your created items. E.g. Integrs, Strings, Structs.
  25. stackTest4();
  26. }
  27. void stackTest5()
  28. {
  29. cout<<dep++<<"\n"<<flush;
  30. int arr[50];
  31. vector<int> v(50); // Its internal array on heap not on stack
  32. stackTest5();
  33. }
  34. void stackTest6()
  35. {
  36. cout<<dep++<<"\n"<<flush;
  37. int arr[50];
  38. vector<int> v(1000); // Its internal array on heap not on stack. Then watch from heap too
  39. stackTest6();
  40. }
  41. void stackTest7()
  42. {
  43. cout<<dep++<<"\n"<<flush;
  44. int arr[1000];
  45. stackTest7();
  46. }
  47. void stackTest8()
  48. {
  49. cout<<dep++<<"\n"<<flush;
  50. int arr[1000000]; // So big for my MACHINE stack limit
  51. stackTest8();
  52. }
  53. void stackTest9(int a[]) // this is array reference. it behaves as if it is just an integer
  54. {
  55. cout<<dep++<<"\n"<<flush;
  56. stackTest9(a);
  57. }
  58. int arr[1000000];
  59. int main()
  60. {
  61. stackTest9(arr);
  62. return 0;
  63. }