CS2304: C++ for Java Programmers CS 2304: Pointers, References, and Memory McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Memory • Where your variables are stored • Where your code is stored McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Types of Memory • Stack Memory • Static memory, no management required • Heap Memory • Dynamic memory, management required • Use new and delete to manage the memory • Code memory • Where code is stored • (not actual name) McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers • Pointer • A variable whose value is a memory address • Internal representation is an integer • Another primitive type (sort of) • Pointee • A value in memory whose address is stored in a pointer; the pointee is the target of the pointer • Valid pointees • Arrays, variables/objects, and functions (the latter is known as a function pointer) • NULL (value of pointer is actually 0) McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers in Graphic Detail int x = 42, y = 99; Address Name:value 0001 x:42 int* p1 = &x; 0002 y:92 int* p2 = &y; 0003 p1:0001 int& r1 = x; 0004 p2:0002 0005 r1:0001 int& r2 = y; 0006 r2:0002 int** p3 = &p2; 0007 p3:0004 McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointer/Reference Operators • Dependent on where they occur in code • * - dereference operator • int c = *myIntegerPointer; • * - pointer declaration (not a name) • int* myIntegerPointer; • & - Address of operator • int* myIntegerPointer = &myInteger; • & - reference declaration (not a name) • int& myIntegerReference = myInteger; • [] – subscript operator McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers n’ stuff • Reference int a = 27; //aRef is a REFERENCE to a • Treated like a variable, int& aRef = a; except it modifies the //aRef2 is also a REFERENCE to a original variable int& aRef2 = aRef; • What’s the effect of //aPt is a pointer to where a is “aRef2++;” int* aPt = &a; • a == 28; //true //aRef3 is ALSO a reference to a • aRef == 28; //true int& aRef3 = *aPt; • aRef2 == 28; //true int aCopy = aRef3; • aPt[0] == 28; //true • *aPt == 28; //true • aCopy == 27; //true McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers n’ stuff (cont) • Arrays are pointers allocated on //declaration of an array stack memory (slight lie, but int foo[5] = {16,2,77,40,8}; 95% true) int* fooPt = foo;//WHAAATTT?! int b = fooPt[2]; //What’s b’s value? • (the other 5%) int& bRef = fooPt[2]; http://www.geeksforgeeks.org/g- fact-5/ bRef = 36;//What’s the contents of foo? • References can be made to array elements McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers to Pointers • Can be convoluted • int a = 27; • int* aPt = &a; • int** aPtPt = &aPt; • Can be 2-D array McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Pointers Pointers • Pointer • Value of a place in memory, that you can access • Reference • Essentially a pointer, but is treated like a variable as opposed to a pointer, and modifies the original variable • Rhyming Couplets type* points the way *var is what it says type& is a sneaky change &var is a pointer exchange McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Dynamic Memory Allocation int theArrayStatic[12];//completely static, legal int x = 0; cin >> x; int theArrayDynamic[x];//dynamic, illegal int* theArrayDynamic = new int[x];//dynamic, legal McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers When to Dynamically Allocate Memory? • When? • When the amount of memory necessary can’t be determined BEFORE run time e.g. dynamically sizing containers • When you need something to last beyond its scope • Note: More often than not, you SHOULD NOT make functions that allocate memory dynamically and instead make use of references/return by reference via function arguments McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Passing Streams to Functions • If you pass a stream to a function, you MUST pass it by reference void foo(ifstream input); // wrong void foo(ifstream& input); // right • Passing a stream by value results in undefined behavior McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Return by Pointer/Reference • Remember: function arguments are passed by value (passed by copy) • References are like pointers, but are syntactically treated like normal variables • Using return by pointer/reference, you’re not copying an ENTIRE object McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Return by Pointer/Reference – Java Misconceptions • http://www.tutorialspoint.com/compile_java_online.php • Java passes by value • Java NEVER gives you an object, it gives you a pointer to an object • Hence the new operator • Java does NOT pass by reference McQuain 2012, Gusukuma 2015
CS2304: C++ for Java Programmers Return by Pointer/Reference • void L07_PointersExample02_0(Dog* myDog); • void L07_PointersExample02_1(Dog& myDog); • Both of these examples can MODIFY the original Dog object • Alternative to creating an object inside the function McQuain 2012, Gusukuma 2015
Recommend
More recommend