Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. int freeRam ()
  2. {
  3. extern int __heap_start, *__brkval;
  4. int v;
  5. return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  6. }
  7.  
  8. void setup() {
  9. // put your setup code here, to run once:
  10. Serial.begin(115200);
  11. recurse(1);
  12. }
  13.  
  14. void loop() {
  15. // put your main code here, to run repeatedly:
  16.  
  17. }
  18.  
  19. void recurse(long i) {
  20. Serial.println(i);
  21. recurse(i+1);
  22. }
  23.  
  24. /*
  25. stack_overflow
  26. - a quick program to force a stack overflow in order to see how many stack frames in a small function can be loaded onto the stack before the overflow occurs
  27.  
  28. By Gabriel Staples
  29. www.ElectricRCAircraftGuy.com
  30. Written: 6 Nov 2017
  31. Updated: 6 Nov 2017
  32.  
  33. References:
  34. - Jumping into C++, by Alex Allain, pg. 230 - sample code here in the chapter on recursion
  35.  
  36. To compile and run:
  37. Compile: g++ -Wall -std=c++11 stack_overflow_1.cpp -o stack_overflow_1
  38. Run in Linux: ./stack_overflow_1
  39. */
  40.  
  41. #include <iostream>
  42.  
  43. void recurse(int count)
  44. {
  45. std::cout << count << "n";
  46. recurse(count + 1);
  47. }
  48.  
  49. int main()
  50. {
  51. recurse(1);
  52. }
  53.  
  54. /*
  55. recursion_until_stack_overflow
  56. - do a quick recursion test to see how many times I can make the call before the stack overflows
  57.  
  58. Gabriel Staples
  59. Written: 6 Mar. 2018
  60. Updated: 7 Mar. 2018
  61.  
  62. References:
  63. - Jumping Into C++, by Alex Allain, Ch. 16: Recursion, p.230
  64. */
  65.  
  66. // Force the compiler to NOT optimize! Otherwise this recursive function below just gets optimized into a count++ type
  67. // incrementer instead of doing actual recursion with new frames on the stack each time. This is required since we are
  68. // trying to force stack overflow.
  69. // - See here for all optimization levels: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
  70. // - They include: -O1, -O2, -O3, -O0, -Os (Arduino's default I believe), -Ofast, & -Og.
  71.  
  72. // I mention `#pragma GCC optimize` in my article here: http://www.electricrcaircraftguy.com/2014/01/the-power-of-arduino.html
  73. #pragma GCC optimize ("-O0")
  74.  
  75. void recurse(unsigned long count) // each call gets its own "count" variable in a new stack frame
  76. {
  77. // delay(1000);
  78. Serial.println(count);
  79.  
  80. // It is not necessary to increment count since each function's variables are separate (so the count in each stack
  81. // frame will be initialized one greater than the last count)
  82. recurse (count + 1);
  83.  
  84. // GS: notice that there is no base condition; ie: this recursive function, once called, will never finish and return!
  85. }
  86.  
  87. void setup()
  88. {
  89. Serial.begin(115200);
  90. Serial.println(F("nbegin"));
  91. // First function call, so it starts at 1
  92. recurse (1);
  93. }
  94.  
  95. void loop()
  96. {
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement