Implementing Parameter Modes
Implementing Parameters: Swap in Ada, C, ...
- Consider swaptest.adb
- Can we write swap in Java?
- Why not: params are all passed by value: copy is passed in and modified
- What happens in C?
- What happens in C++?
Parameter Passing Mechanism vs Mode
- In C and C++, the language specifies the param passing mechanism
- In Ada, the language specifies the parameter passing mode
- The mechanism is left to the compiler writer
Techniques for Implementing Modes
- Goals:
- Make out mode possible
- Efficiency: space and time
- Security
- Minimize aliases
- Two possible ways of implementing in out mode
- Copy in/copy out (aka by pass by value-result):
- actual parameter has a separate memory location
- on procedure call: value of
formal actual param is copied to
actual formal parameter
- changes to actual parameter are made to the copy (ie to the separate location)
- on procedure exit: value of formal parameter is copied back to the actual
- for in mode (ie pass by value): actual is copied into formal, but nothing is copied back
- for out mode (ie pass by result): formal is copied back to actual, but nothing is copied in
- Reference (aka pass by reference):
- on procedure call: formal parameter receives the address of the actual
- formal is alias for actual
- any changes to formal parameter are actually changes to the actual parameter
- Examples:
- What about the time and space efficiency of these implementation techniques
Implementing Modes In Ada
- Means of implementation affected by type
- In mode can simply be copy in
- Reference sometimes used to reduce copying for
larger types
- In and In Out mode must be reference or copy in/out
- Elementary types (ie type that have no components, like
integers) are passed by copy
- Composite types that we will use (eg array of integers)
may be passed by copy or reference
- Composite types whose elements are pointers
are passed by reference
- Some types which we won't use (eg tagged, task, protected)
are also passed by reference