CarranoChap2 - Carrano CS 150 Carrano CS 150 11 Program Memory Allocation Program B’s Run-Time Stack r Room for B’s growth Program B’s Data

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Carrano - CS 150 Carrano - CS 150 11 Program Memory Allocation Program B’s Run-Time Stack r ( Room for B’s growth ) Program B’s Data Heap Program B Program A’s Run-Time Stack r ( Room for A’s growth ) Program A’s Data Heap Program A Operating System Each program being executed on a computer is allocated extra space which it could use for its stack of function calls, and/or for “dynamically” expanding its use of memory for data. The memory used for data is known as a “heap”. Carrano - CS 150 Carrano - CS 150 22 A Program’s Run-Time Stack Program XYZ: void main() { int snap = 7; int yap = 10; int blap = 100; blap = snippy(snap); yippy(yap, blap); zippy(yap); } int snippy(int snoo) { int hep = yippy(snoo,5); if (hep > 0) return hep; else return 0; } void yippy(int &yoo, int boo) { int i; for (i = 1; i < boo; i+ +) yoo += i; return; } void zippy(int zoo) { cout << zoo << endl; } snap: 7 yap: 10 blap: 100 void main() { int snap = 7; int yap = 10; int blap = 100; blap = snippy(snap); yippy(yap, blap); zippy(yap); } snoo: 7 hep: ?? ? int snippy(int snoo) { int hep = yippy(snoo,5); if (hep > 0) return hep; else return 0; } yoo: boo: 5 int yippy(int &yoo, int boo) { int i; for (i = 1; i < boo; i++) yoo += i; return boo + yoo; } i: 5 Carrano - CS 150 Carrano - CS 150 33 Recursion It is sometimes useful to have a function call itself, making it a recursive function. This approach often enables the programmer to solve a problem by repeatedly solving smaller, more manageable versions of the problem, until the original problem’s solution becomes apparent. Care must be taken when using recursion, however. Because a new incarnation of the function is placed upon the run-time stack each time the function is recursively called, there is a danger that the stack will overflow. To prevent this from happening, the function must contain a termination condition, which will recognize when the recursion should halt, and pop the current incarnation of the Carrano - CS 150 Carrano - CS 150 44 Recursion Example #1: Factorial ///////////////////////////////////////////////////////////////////////// /// // Program file: factorial.cpp // // This program queries the user for a non-negative integer and then uses // // a recursive function to calculate the factorial of the input value. // ///////////////////////////////////////////////////////////////////////// /// #include <iostream> #include <iomanip> using namespace std; int factorial(int n); //////////////////////////////////////////////////////////// // The main function asks the user for a value, calls the // // recursive factorial function, and outputs the result. // //////////////////////////////////////////////////////////// void main() { int number, result; cout << "Enter a non-negative integer for factorial computation: ";...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 150 taught by Professor Klein during the Spring '08 term at Southern Illinois University Edwardsville.

Page1 / 32

CarranoChap2 - Carrano CS 150 Carrano CS 150 11 Program Memory Allocation Program B’s Run-Time Stack r Room for B’s growth Program B’s Data

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online