3
SPL/2010 Parameter passing in Java ● Myth: “Objects are passed by reference, primitives are passed by value” ● Truth #1: Everything in Java is passed by value. (Objects, are never passed at all) ● Truth #2: The values of variables are always primitives or references, never objects 3

4
SPL/2010 ● Pass-by-value ● actual parameter is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. ● location is typically a chunk of memory on the runtime stack for the application 4

5
SPL/2010 ● Pass-by-reference ● formal parameter merely acts as an alias for the actual parameter. ● anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter 5

7
SPL/2010 foo(d); ● passes the value of d to foo; it does not pass the object that d points to! ● The value of the pointer being passed is similar to a memory address. ● The value uniquely identifies some object on the heap. 7

10
SPL/2010 ● The primitive value of a parameter is set to the value of another parameter ● the value "0" was passed into the method giveMeTen, not the variable itself. ● same is true of reference variables - value of reference is passed in, not the variable itself 10

12
SPL/2010 In C++ ● both primitives and objects may be allocated on stack or heap. ● anything allocated on the heap can be reached by using a reference to its location ● reference = pointer - holds a memory address 12

13
SPL/2010 Pointers ● pointers are primitive types themselves ● hold memory address of a primitive or an object which resides either on the heap or on the stack ● pointer to type type_a is of type type_a * ● type_a * is a primitive type ● we can have a pointer to any type ● pointer to a pointer to type_a: type_a **. 13

16
SPL/2010 Operator * ● operator *: whose value is the content of the memory to which the pointer points 16

17
SPL/2010 Process memory 17

18
SPL/

19
SPL/2010 Access through pointers ● member methods of class T are executed always as T *- instance location in memory ● method of an object does not change the object's internal state, method is const ● operator -> access members and methods via pointer to object ● access members / methods of an object not through pointer using (.) dot-operator 19

20
SPL/2010 instantiate some cows 20

21
SPL/2010 Analysis - bety ● space for Cow is allocated on activation frame of main function. ● constructor of Cow is called with ● this points to the address of the space allocated on the stack. ● space allocated is associated with variable bety 21

22
SPL/2010 Analysis - ula ● space for a pointer Cow * is allocated on the activation frame of the main function. ● space allocated is associated with the variable ula. ● space for a Cow is allocated on the heap (using new operator) ● constructor is called with ● this points to the address of the space allocated on the heap. ● address of allocated Cow is saved in ula. 22

26
SPL/2010 Reference ● is basically a const pointer without using any pointer notations. ● may only be assigned once, when it is declared (initialization of reference) ● may not be altered to reference something else later. 26

27
SPL/

28
SPL/2010 const pointers ● Any type in C++ can be marked as const, which means that its value cannot be changed ● const pointer is declared by adding the const keyword after the type ● int *const i_ptr: a const pointer to an int ● cannot have references to references (this is explicitly illegal) 28

29
SPL/2010 Parameter Passing ● all parameters are either 'in‘ or 'out' ● 'in' parameters - information passed to the function, which the function does not change. ● operation on 'in' parameter - not visible outside ● 'out' parameters are a side-channel for function to return information, in addition to return value. ● changes made to 'out' parameters are visible outside the scope of the function. 29

33
SPL/2010 By Value ● call byVal - both 30 and the entire content of hemda are copied ● placed on the activation frame of byVal ● byVal performs all of its operations on these local copies - no changes to hemda 33

34
SPL/2010 By Pointer ● output =30 34

35
SPL/2010 By Pointer ● byPointer received a pointer to location of hemda on activation frame of calling function ● changed its id 35

38
SPL/2010 When to Use Each Form of Parameter Passing? ● passing parameters by value comes with a cost - copying and constructing a new object ● change a parameter outside the scope of the local function: by-reference or by-pointer 38

39
SPL/2010 Recommendations ● For a function that uses passed data without modifying it (In parameter): ● If data object is small (built-in data type or a small structure) - pass it by value. ● If data object is an array, use a pointer because that's your only choice. Make the pointer a pointer to const. ● If the data object is a good-sized structure, use a const reference. ● If the data object is a class object, use a const reference 39

40
SPL/2010 Recommendations ● For a function that modifies data in the calling function (Out parameter): ● If the data object is a built-in data type, use a pointer or a reference, prefer the later. ● If the data object is an array, use your only choice, a pointer. ● If the data object is a structure, or a class object, use a reference 40

42
SPL/2010 Returning Values From Functions ● values can be returned: ● either by value (copy) ● by reference* ● by pointer* (*) when returning something by reference or pointer care should be taken ● Is it inside activation frame to be demolished ? 42

43
SPL/2010 ● Returning a reference / pointer to an invalid address on the stack is one of the main pitfalls of C++ beginners. 43

46
SPL/2010 Memory management ● This is totally bad as we can not predict how our program will work! ● No flag is lifted for us,a.k.a no exception, no segmentation fault. ● It works every time differently. 46

47
SPL/2010 C++ Arrays ● blocks of continuous memory ● store data of the same type. ● memory image of an array of integers which holds the number 5 in each cell ● cell is of size 4 bytes 47

52
SPL/2010 array of pointers ● allocate a new Cow object on the heap, and store a pointer to it in a cell of Cow* ● delete [] calls destructor of elements in array ● each element in the array is a pointer - destructor of a pointer is a nop ● individual Cows will not be deleted ● delete [] deallocates memory allocated by new [] ● delete each Cow – before deleting the array! 52

53
SPL/2010 Arrays on the Stack ● array's size must be known in advance: ● Cow cow_arr[5]; ● initialize individual Cows: ● Cow cow_arr[5] = {Cow(1), Cow(21), Cow(454), Cow(8), Cow(88)}; ● accessing cells of the array on the Stack same as through a pointer ● cow_arr is basically a pointer to the beginning of the array of Cows on the Stack. 53