[CSUSB]
>> [CNS]
>> [Comp Sci Dept]
>> [R J Botting]
>> [CSci620]
>>
10
[Source]
Many run time errors are about running out of memory or
accessing parts of memory that we shouldn't touch. Most of
the rest of them are when we pass parameter to a function
with a bad value: dividing by zero, square root of a negative
number, etc.
Functions and Procedures
In Computer science: functions have a value and Procedure
do things.
Activation Records
In FORTRAN each function and subprogram had one
and only one activation record. It was allocated as
static storage when the program was loaded. Recursion
can not work.
Modern language maintain a Run-time stack that has all active activation records on it.
Dynamic pointers indicate where a function or procedure was called.
IMHO (In my humble opinion) displays are not common. Instead
all activation records are put on a stack, BUT each
one has an extra static pointer that indicates the
activation record of the subprogram where this subporgram
was declared.
Parameter Passing
Modern terminology is to use 'pass by' not 'call by'.
. . . . . . . . . ( end of section Topics) <<Contents | Index>>
Questions
C++ notation: call by reference is shown in the header by 'T & x`.
In Java: primitive types like int and 'double' are passed by value
but Objects are passed by reference.
On page 130..131 what does t:=1*1 etc mean
This is an attempt to show the way a value is returned to the
calling code.
In reality, programs have a run time stack that is used to handle expressions: values are pushed on to it, operators pop them off. The same stack is also used for activation records, and so when a function returns a value the activation record is popped of the stack and the reurned value left in its place ready to be used in an expression.
In figure 5.16 page 132, why is AR2 in front of AR1 for p2?
Because the second AR is pushed on to the stack and so is on top
(in front of) the previous one.
What about memory boundaries and recursion
An uncontrolled recursion will often fill the memory with
ARs. When the stack of ARs fills the available memory
the program is forced to halt with an error.
What is the difference between heap and stack overflow.
When either overflows we either have to get more space or
halt. So either can trigger garbage collection.
Stack overflow happens because we call to many functions without returning from them. This is typically when we write a bad recursive function.
Heap overflow occurs when we allocate two much data with deallocating
it. This tends to be when we forget to collect garbage.
Explain Call by value-result
This means that the value of the formal parameter is
the initial value of the formal parameter, and the final value
of the formal parameter replaces the old actual parameter's value.
It has little to reccomend it compare with call-by-reference.
Do you have a complete list of parameter passing methods
It is just like any call: create a new AR, fill in the data.....
How can you change the value of a varible passed by value
You can;t. All changes are on a local copy.
What is a functions's signature
A function's signature is a description of the types of it's
parameters + sometimes, type of returned data.
When should you use call by reference vs call by value.
Call be value is best used for primitive data types.
Pass arrays, records, all objects and ponters to objects by
reference.
How do detect a run away pointer?
You find out about it when a program crashes.
In a few language each pointer is given a piece of storage over which it is allowed to point. This makes it easy to to test each use of the pointer to see if it is out of range.
Many modern machines isolate access to memory to special segments. This can also detect a pointer that is out of range with a 'Segment overflow' or 'general protection fault' error.
It is best to be able to prove that all your pointers are going somewhere sensible.
It is 4am in the morning: do you know where your poiner is?
How does a procedural language from an imperative one?
There is no real difference. Procedural means 'step by step'
and imperative means 'using commands'.
What happens with FORTRAN and COBOL not checking boundaries.
(1) the ran faster.
(2) clever programmers invent clever tricks that work and exploited the lack of boundaries.
(3) stupid programs made life very interesting for themselves and others by creating fun bugs.
(4) normal programmers exercised care and had no problems.
. . . . . . . . . ( end of section Questions) <<Contents | Index>>
Laboratory
Parameter passing and recursive activation records:
[ lab10.html ]
Next
[ 11.html ]
. . . . . . . . . ( end of section CS620 Session 10 Subprograms) <<Contents | Index>>
Glossary