Presentation is loading. Please wait.

Presentation is loading. Please wait.

M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS498-006 Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,

Similar presentations


Presentation on theme: "M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS498-006 Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,"— Presentation transcript:

1 M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS498-006 Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records, etc.

2 M M EECS498 EECS498 Andrew M Morgan2 Review: Pass-By-Value A value parameter in a function becomes a copy of the argument that is passed in Changing the value of a value parameter: – Does changes the memory associated with the parameter –Does not change the memory associated with the argument passed in void valFunc(float val) { val = 50; } int main() { int mainVal = 9; valFunc(mainVal); cout << "mainVal: " << mainVal << endl; return (0); } mainVal: 9 This assignment changes the memory associated with this variable (only)! (Main's mainVal is unaffected)

3 M M EECS498 EECS498 Andrew M Morgan3 Review: Pass-By-Reference A reference parameter in a function "references" the same physical memory location as the argument passed in Changing the value of a reference parameter: –Does not change the memory associated with the parameter – Does change the memory associated with the argument passed in –Therefore – argument's memory must not be constant or literal void refFunc(float &val) { val = 50; } int main() { int mainVal = 9; refFunc(mainVal); cout << "mainVal: " << mainVal << endl; return (0); } mainVal: 50 This assignment changes the memory associated with this variable!

4 M M EECS498 EECS498 Andrew M Morgan4 Pre-Existing Functions C++ libraries contain many functions that you usually do not have to write algorithms for Many math related functions in include file (sqrt,pow,etc) Example: Pseudo-random number generation –Pseudo-random numbers are always generated in a sequence –The same sequence always results from the same starting value –Modifying the starting value changes the sequence – called a "seed" –Must #include to access these functions –Set seed with function "srand" void srand(unsigned int seedValue); Example: srand(100); //sets the seed to begin pseudo-random #s to be 100 –Generate a pseudo-random number with "rand" int rand(); Returns an integer between 0 and constant "RAND_MAX" (usually 32,767)

5 M M EECS498 EECS498 Andrew M Morgan5 Pseudo-Random Numbers, Example Program #include //req'd for srand and rand #include //req'd for cout and cin using namespace std; int main() { double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; cout << "Enter seed: "; cin >> seed; srand(seed); for (i = 0; i < 10; i++) { randVal = rand() % (maxX - minX + 1) + minX; cout << randVal << endl; } for (i = 0; i < 10000; i++) { avg += rand() % (maxX - minX + 1) + minX; } avg /= 10000; cout << "Avg of 10000: " << avg << endl; return (0); } srand() is usually called only one time to start a sequence rand() is called each time a pseudo-random number is needed

6 M M EECS498 EECS498 Andrew M Morgan6 Pseudo-Random Numbers, Example Output [ 34 ] temp -:./a.out Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 [ 35 ] temp -:./a.out Enter seed: 1652 38 32 43 36 48 34 48 31 49 Avg of 10000: 40.0484 [ 36 ] temp -:./a.out Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 Note: Same seed = same sequence = same results

7 M M EECS498 EECS498 Andrew M Morgan7 Default Parameters To Functions If a function's parameter is usually a known value, the parameter can be given a "default value" –Default parameters must be at the end of the parameter list –Default values specified in function prototype –Argument is not required for default parameters Default value will be used if no argument is provided void print(int a, int b = 4); int main() { print(8, 16); print(7); //print(); //Will not compile! return (0); } void print(int a, int b) { cout << a << " " << b << endl; } 8 16 7 4 Default parameter (default value is 4)

8 M M EECS498 EECS498 Andrew M Morgan8 Types of Languages Machine Language – Actually stored in memory –Platform dependent –Binary instructions only –1001001110110101 Assembly Language – Direct mapping to machine language –Platform dependent –Basic codes, each of which maps to a sequence of binary digits (bits) –ADD R1, IP High-Level Language – Easy to write, similar to English –Platform independent –Each platform needs to be able to convert to machine language –area = PI * square(radius);

9 M M EECS498 EECS498 Andrew M Morgan9 Creating An Executable With C++ Create C++ program with extension.cpp Pre-processor –"pastes" prototypes and definitions from include files –Controlled via pre-processor directives that begin with "#" –Results in C++ code that has been modified based on directives Compiler –Converts C++ code into assembly and/or machine language –Usually called "object code" with extension.o –Leaves "holes" in place of function calls from other libraries Linker –Fills holes from compiler with locations (addresses) of library functions –Results in complete sequence of machine language Result: Executable program –Can be executed on the platform in which it was compiled and linked –Sometimes, all steps are combined, so individual steps are transparent to user

10 M M EECS498 EECS498 Andrew M Morgan10 Global and Static Global constants and variables –Declared outside the scope of any individual function –Globals can be accessed from anywhere –Memory is obtained at beginning of program, is freed up when the program is finished – Global variables must not be used in this course!!! Static variables –Declared within a function, using keyword "static" before data type –Memory is obtained at beginning of program, is freed up when the program is finished –Can only be accessed from within the function it is declared –Scope is within the function it is declared, lifetime is the entire program

11 M M EECS498 EECS498 Andrew M Morgan11 Global and Static, Example //Global variable to count total calls int numFuncCalls; int f1() { //Allocated and initialized once static int f1Calls = 0; numFuncCalls++; f1Calls++; cout << "In f1: totalCalls: " << numFuncCalls << " f1 calls: " << f1Calls << endl; } int f2() { //Allocated and initialized every //time the f2 is called => local to f2 int f2Calls = 0; numFuncCalls++; f2Calls++; cout << "In f2: totalCalls: " << numFuncCalls << " f2 calls: " << f2Calls << endl; } int main() { //Initialization of global var numFuncCalls = 0; f1(); f2(); f1(); f2(); //numFuncCalls can be accessed from //anywhere, since it is global cout << "End of main: " << "totalCalls: " << numFuncCalls << endl; //This will not compile! Scope of //f1Calls is ONLY in f1 function //cout << "f1 Calls: " << f1Calls; return (0); } In f1: totalCalls: 1 f1 calls: 1 In f2: totalCalls: 2 f2 calls: 1 In f1: totalCalls: 3 f1 calls: 2 In f2: totalCalls: 4 f2 calls: 1 End of main: totalCalls: 4 Note: Does not count f2 calls!

12 M M EECS498 EECS498 Andrew M Morgan12 Use Of Memory Following is a generic diagram of the organization of main memory –Not guaranteed that every platform uses this exact ordering CPU: executes instructions, one at a time memory reserved for operating system compiled and linked C++ program (in binary form) heap: the global and static data for your C++ program; data whose lifetime is the entire program run runtime stack: the local data for the individual functions in your C++ program; lifetime is while function is active reads instructions and values from memorywrites results back to memory Room To Grow MAIN MEMORY

13 M M EECS498 EECS498 Andrew M Morgan13 Activation Records Every time a function is called, many pieces of information the function requires need to be stored in an "activation record" (AR) As one function calls another, ARs stack up on top of each other When a function ends, the AR on top of the stack is removed –Includes the parameters and local variables Following is one possible version of an AR return value return address dynamic link parameters local variables value returned by function, if any address of 1st instruction in caller after function call address of top of calling function's AR parameters passed into the called function variables declared in the called function as non-static

14 M M EECS498 EECS498 Andrew M Morgan14 Memory Trace, p.1 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 0 ??? calls heap main's AR mainVar Subset Of Memory red statement represents the next one to be executed

15 M M EECS498 EECS498 Andrew M Morgan15 Memory Trace, p.2 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 0 ??? 10 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 heap

16 M M EECS498 EECS498 Andrew M Morgan16 Memory Trace, p.3 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 0 ??? 10 ??? 10 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 inFunc's AR locIn val heap

17 M M EECS498 EECS498 Andrew M Morgan17 Memory Trace, p.4 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 0 ??? 10 60 ??? 10 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 inFunc's AR locIn val heap

18 M M EECS498 EECS498 Andrew M Morgan18 Memory Trace, p.5 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 1 ??? 10 60 ??? 10 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 inFunc's AR locIn val heap

19 M M EECS498 EECS498 Andrew M Morgan19 Memory Trace, p.6 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 1 60 10 60 ??? 10 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 inFunc's AR locIn val heap

20 M M EECS498 EECS498 Andrew M Morgan20 Memory Trace, p.7 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 1 ??? 10 60 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 heap

21 M M EECS498 EECS498 Andrew M Morgan21 Memory Trace, p.8 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 1 ??? 10 60 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 ??? 60 ??? inFunc's AR locIn val heap

22 M M EECS498 EECS498 Andrew M Morgan22 Memory Trace, p.9 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 1 ??? 10 60 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 ??? 60 110 ??? inFunc's AR locIn val heap

23 M M EECS498 EECS498 Andrew M Morgan23 Memory Trace, p.10 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 2 ??? 10 60 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 ??? 60 110 ??? inFunc's AR locIn val heap

24 M M EECS498 EECS498 Andrew M Morgan24 Memory Trace, p.11 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 2 ??? 10 60 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 110 60 110 ??? inFunc's AR locIn val heap

25 M M EECS498 EECS498 Andrew M Morgan25 Memory Trace, p.12 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 2 ??? 10 110 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 ??? heap

26 M M EECS498 EECS498 Andrew M Morgan26 Memory Trace, p.13 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 3 ??? 10 110 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 ??? heap

27 M M EECS498 EECS498 Andrew M Morgan27 Memory Trace, p.14 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 3 ??? 10 110 ??? calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code outFunc's AR locOut p1 110 heap

28 M M EECS498 EECS498 Andrew M Morgan28 Memory Trace, p.15 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 3 ??? 110 calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code heap

29 M M EECS498 EECS498 Andrew M Morgan29 Memory Trace, p.16 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout 3 0 110 calls main's AR mainVar Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code heap

30 M M EECS498 EECS498 Andrew M Morgan30 Memory Trace, p.17 return value return addr dynamic link parameters local variables int calls; //Global var (ack!) int inFunc(int val) { int locIn; locIn = val + 50; calls++; return (locIn); } int outFunc(int p1) { int locOut; locOut = inFunc(p1); locOut = inFunc(locOut); calls++; return (locOut); } int main() { int mainVar; calls = 0; mainVar = outFunc(10); return (0); } AR Layout Subset Of Memory red statement represents the next one to be executed dashed arrows point to conceptual locations in code Program Finished!


Download ppt "M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS498-006 Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,"

Similar presentations


Ads by Google