Pointers and Parameter Passing in C++

Pointers and Parameter Passing in C++

Pointers and Parameter Passing in C++ SPL/2010 1 In Java Primitive types (byte, short, int) allocated on the stack

Objects allocated on the heap SPL/2010 2 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 SPL/2010 3 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 SPL/2010 4 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 SPL/2010 5 public void foo(Dog d) { d = new Dog("Fifi"); // creating the "Fifi" dog

} Dog aDog = new Dog("Max"); // creating the "Max" dog // at this point, aDog points to the "Max" dog foo(aDog); // aDog still points to the "Max" dog SPL/2010 6 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. SPL/2010 7 passing a reference by value Object x = null; giveMeAString (x); System.out.println (x); void giveMeAString (Object y) {

y = "This is a string"; } SPL/2010 8 passing a reference by value int x = 0; giveMeATen (x); System.out.println (x); void giveMeATen (int y) { y = 10; } SPL/2010 9

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 SPL/2010

10 Dog myDog = new Dog("Rover"); foo(myDog); Suppose the Dog object resides at memory address 42. This means we pass 42 to foo(). public void foo(Dog someDog) { someDog.setName("Max"); // AAA someDog = new Dog("Fifi"); // BBB someDog.setName("Rowlf"); // CCC } SPL/2010 11 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 SPL/2010 12

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 **. SPL/2010 13 SPL/2010 14 Code analysis

1. 2. 3. 4. 5. space for primitive of type int* allocated on activation frame of main function space allocated is associated with variable i_ptr space for primitive of type int is allocated on heap (using new), initialized to 10 address of newly allocated integer is saved

in i_ptr. operator << is passed content (by value) i_ptr points to. SPL/2010 15 Operator * operator *: whose value is the content of the memory to which the pointer points SPL/2010 16

Process memory SPL/2010 17 SPL/2010 18 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 SPL/2010 19 instantiate some cows

SPL/2010 20 Analysis - bety space for Cow is allocated on activation frame of main function. constructor of Cow is called with 482528404

this points to the address of the space allocated on the stack. space allocated is associated with variable bety SPL/2010 21 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 834579343 this points to the address of the space allocated on the heap. address of allocated Cow is saved in ula. SPL/2010 22

Process memory SPL/2010 23 Dereferencing /"Address Of dereference a pointer (* operator): (*ula).moooo();

take the address of something (& operator): int i = 10; int *i_ptr = &i; i_ptr holds the address in which i is stored on the stack SPL/2010

24 pass pointer arguments to functions SPL/2010 25 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. SPL/2010 26 SPL/2010 27 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) SPL/2010

28 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. SPL/2010 29 Parameter Passing - Java 2 forms of passing parameters to methods,

primitives are passed by value objects by reference (possible 'out' parameters). SPL/2010 30 Parameter Passing C++ 3 forms for parameter passing

By value, for 'in' parameters. By pointer, for 'out' parameters. By reference, for 'out' parameters. SPL/2010 31 By Value

outputs = 20 SPL/2010 32 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 SPL/2010 33 By Pointer output =30 SPL/2010 34 By Pointer

byPointer received a pointer to location of hemda on activation frame of calling function changed its id SPL/2010 35 By Reference output =30

SPL/2010 36 By Reference refrain from using pointers inherently unsafe - easily cast to other types compiler is allowed to optimize the reference beyond the "const pointer" abstraction

SPL/2010 37 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 bypointer

SPL/2010 38 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 SPL/2010 39 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 SPL/2010 40 Recommendations

When receiving a pointer check pointer for nullity. (A reference cannot be null.) SPL/2010 41 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 ?

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

different level of compiler optimizations g++ 1.cpp; ./a.out 0xbffff564 134513864 0xbffff564 134513864 g++ 1.cpp -O1; ./a.out 0xbffff564 1 0xbffff564 2

g++ 1.cpp -O2 ; ./a.out 0xbffff560 1 0xbffff564 134519000 g++ 1.cpp -O3 ; ./a.out 0xbffff574 1 0xbffff570 1 SPL/2010

45 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. SPL/2010

46 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 SPL/2010 47 C++ Arrays Accessing individual cells dereferencing a pointer to the specific cell. assume int *arr_ptr:

access fourth cell: arr_ptr[3] = *(arr_ptr+3) pointer arithmetic: arr_ptr+3 = arr_ptr+3 x sizeof(int) add/subtract numbers from pointer implicitly multiplied by size of data of pointer type SPL/2010 48 Arrays on the Heap everything in C++ may be allocated on

Stack or Heap allocate array on heap: new [] operator deallocating an array: delete [] operator SPL/2010 49

output = 0 new [] operator initialize array's elements by calling their default constructor (int - 0). SPL/2010 50 array of pointers SPL/2010 51 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! SPL/2010 52 Arrays on the Stack array's size must be known in advance:

initialize individual Cows: Cow cow_arr[5]; Cow cow_arr[5] = {Cow(1), Cow(21), Cow(454), Cow(8), Cow(8 8)}; 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. SPL/2010

53

Recently Viewed Presentations

  • Ch. 2 Communication &amp; Culture

    Ch. 2 Communication & Culture

    Intercultural Communication. Defined:the process occurring when members of 2 or more cultures or co-cultures exchange messages influenced by different cultural perceptions & symbol systems, both verbal & nonverbal.(Samovar, et al 2007) Not all differences are important , and many can...
  • NETBACKUP Process flow - Veritas

    NETBACKUP Process flow - Veritas

    NetBackup start programs on remote hosts (can be UNIX clients. or other servers). For example, the server can connect to UNIX. clients without requiring /.rhosts entries on the remote host. The program is used when nbjm starts bpbrm and when...
  • 16th Century England - Woodland Hills School District

    16th Century England - Woodland Hills School District

    "In 16th-century England, the habit of emptying chamber pots out of upper-story windows into the gutter made a city stroll so hazardous that gentlemen gallantly took the side nearest the curb when walking with their ladies--a position they have assumed...
  • Food Production Macronutrients- carbohydrates, proteins and fats Micronutrients-

    Food Production Macronutrients- carbohydrates, proteins and fats Micronutrients-

    Examples of GMO crops. Golden Rice: beta carotene gene from daffodils inserted into rice plants. Beta carotene can be converted into Vitamin A in the body. Round-up Ready (glyphosphate resistant): Gene for resistant to the herbicide is inserted to allow...
  • http://people.maths.ox.ac.uk/nanda/perseus/index.html If you use it, cite it Click

    http://people.maths.ox.ac.uk/nanda/perseus/index.html If you use it, cite it Click

    Create your data file. Note first 2 rows contain the information described below Your data points plus extra column = starting radius r. Number of coordinates (i.e., number of columns in your original data set).
  • Ancient Egypt Test Study Guide Answers

    Ancient Egypt Test Study Guide Answers

    Vocabulary. Pyramid - huge triangular tomb build by the Egyptians and other peoples. Hieroglyphics -the ancient Egyptian writing system that used picture symbols. Papyrus - long-lasting paper like material made from reeds that the ancient Egyptians used to write on
  • DELTA AIRLINES: Product Market Analysis Paula Campbell John

    DELTA AIRLINES: Product Market Analysis Paula Campbell John

    DELTA AIRLINES: Product Market Analysis Paula Campbell John Paulk Kevin Wood Group B2 Changes in Market Overall Product Locations served Major Players New Technology Delta's Goals New Enterprises Terrorist Attacks. 9/11 and Shoe Bomber Decline in Market- Causes.
  • La Géométrie des images

    La Géométrie des images

    La Géométrie des images Principes physiques Définitions La datation L'orbite des Satellites L'attitude des Satellites Les directions de visée, la géométrie des capteurs Formulaire Modéliser la prise de vue Définitions 3 grands types de modèles Le modèle physique de prise...