m the university of michigan andrew m. morgan andrew m morgan1 eecs498-006 lecture 03 savitch ch....

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

Upload: richard-blackburn

Post on 26-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 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,

Andrew M Morgan 1

M The UniversityOf Michigan

Andrew M. Morgan

EECS498-006 Lecture 03

Savitch Ch. 3-4, MiscMore on Functions

Memory, Activation Records, etc.

Page 2: 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,

M

Andrew M Morgan 2 M

EECSEECS498498

EECSEECS498498

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)

Page 3: 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,

M

Andrew M Morgan 3 M

EECSEECS498498

EECSEECS498498

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 memoryassociated with this variable!

Page 4: 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,

M

Andrew M Morgan 4 M

EECSEECS498498

EECSEECS498498

Pre-Existing Functions

• C++ libraries contain many functions that you usually do not have to write algorithms for

• Many math related functions in <cmath> 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 <cstdlib> 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)

Page 5: 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,

M

Andrew M Morgan 5 M

EECSEECS498498

EECSEECS498498

Pseudo-Random Numbers, Example Program#include <cstdlib> //req'd for srand and rand#include <iostream> //req'd for cout and cinusing 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 onlyone time to start a sequence

rand() is called each time apseudo-random numberis needed

Page 6: 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,

M

Andrew M Morgan 6 M

EECSEECS498498

EECSEECS498498

Pseudo-Random Numbers, Example Output

[ 34 ] temp -: ./a.outEnter seed: 1242464041404332383142Avg of 10000: 39.9971

[ 35 ] temp -: ./a.outEnter seed: 165238324343364834483149Avg of 10000: 40.0484

[ 36 ] temp -: ./a.outEnter seed: 1242464041404332383142Avg of 10000: 39.9971

Note: Same seed = same sequence = same results

Page 7: 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,

M

Andrew M Morgan 7 M

EECSEECS498498

EECSEECS498498

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 167 4

Default parameter(default value is 4)

Page 8: 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,

M

Andrew M Morgan 8 M

EECSEECS498498

EECSEECS498498

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);

Page 9: 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,

M

Andrew M Morgan 9 M

EECSEECS498498

EECSEECS498498

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

Page 10: 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,

M

Andrew M Morgan 10 M

EECSEECS498498

EECSEECS498498

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

Page 11: 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,

M

Andrew M Morgan 11 M

EECSEECS498498

EECSEECS498498

Global and Static, Example//Global variable to count total callsint 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: 1In f2: totalCalls: 2 f2 calls: 1In f1: totalCalls: 3 f1 calls: 2In f2: totalCalls: 4 f2 calls: 1End of main: totalCalls: 4

Note: Does not count f2 calls!

Page 12: 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,

M

Andrew M Morgan 12 M

EECSEECS498498

EECSEECS498498

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

MAINMEMORY

Page 13: 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,

M

Andrew M Morgan 13 M

EECSEECS498498

EECSEECS498498

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

Page 14: 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,

M

Andrew M Morgan 14 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

0

???

???

callsheap

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

Page 15: 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,

M

Andrew M Morgan 15 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

0

???

10

???

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

heap

Page 16: 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,

M

Andrew M Morgan 16 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

0

???

10

???

???

10

???

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

inFunc's AR

locInval

heap

Page 17: 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,

M

Andrew M Morgan 17 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

0

???

10

60

???

10

???

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

inFunc's AR

locInval

heap

Page 18: 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,

M

Andrew M Morgan 18 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

1

???

10

60

???

10

???

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

inFunc's AR

locInval

heap

Page 19: 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,

M

Andrew M Morgan 19 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

1

60

10

60

???

10

???

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

inFunc's AR

locInval

heap

Page 20: 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,

M

Andrew M Morgan 20 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

1

???

10

60

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

heap

Page 21: 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,

M

Andrew M Morgan 21 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

1

???

10

60

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

???

60

???

???

inFunc's AR

locInval

heap

Page 22: 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,

M

Andrew M Morgan 22 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

1

???

10

60

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

???

60

110

???

inFunc's AR

locInval

heap

Page 23: 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,

M

Andrew M Morgan 23 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

2

???

10

60

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

???

60

110

???

inFunc's AR

locInval

heap

Page 24: 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,

M

Andrew M Morgan 24 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

2

???

10

60

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

110

60

110

???

inFunc's AR

locInval

heap

Page 25: 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,

M

Andrew M Morgan 25 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

2

???

10

110

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

???

heap

Page 26: 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,

M

Andrew M Morgan 26 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

3

???

10

110

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

???

heap

Page 27: 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,

M

Andrew M Morgan 27 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

3

???

10

110

???

???

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

outFunc's AR

locOutp1

110

heap

Page 28: 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,

M

Andrew M Morgan 28 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

3

???

110

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

heap

Page 29: 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,

M

Andrew M Morgan 29 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

3

0

110

calls

main's AR

mainVar

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

heap

Page 30: 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,

M

Andrew M Morgan 30 M

EECSEECS498498

EECSEECS498498

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);}

ARLayout

Subset Of Memory

red statementrepresents thenext one to beexecuted

dashed arrowspoint toconceptuallocations in code

Program Finished!