CS 2304: Pointers, References, and Memory McQuain 2012, Gusukuma - - PowerPoint PPT Presentation

cs 2304 pointers references and memory
SMART_READER_LITE
LIVE PREVIEW

CS 2304: Pointers, References, and Memory McQuain 2012, Gusukuma - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS2304: C++ for Java Programmers

CS 2304: Pointers, References, and Memory

McQuain 2012, Gusukuma 2015

slide-2
SLIDE 2

CS2304: C++ for Java Programmers

Memory

  • Where your variables are stored
  • Where your code is stored

McQuain 2012, Gusukuma 2015

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

CS2304: C++ for Java Programmers

Pointers in Graphic Detail

McQuain 2012, Gusukuma 2015

int x = 42, y = 99; int* p1 = &x; int* p2 = &y; int& r1 = x; int& r2 = y; int** p3 = &p2;

Address Name:value 0001 x:42 0002 y:92 0003 p1:0001 0004 p2:0002 0005 r1:0001 0006 r2:0002 0007 p3:0004

slide-6
SLIDE 6

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

slide-7
SLIDE 7

CS2304: C++ for Java Programmers

Pointers n’ stuff

int a = 27; //aRef is a REFERENCE to a int& aRef = a; //aRef2 is also a REFERENCE to a int& aRef2 = aRef; //aPt is a pointer to where a is int* aPt = &a; //aRef3 is ALSO a reference to a int& aRef3 = *aPt; int aCopy = aRef3;

  • Reference
  • Treated like a variable,

except it modifies the

  • riginal variable
  • What’s the effect of

“aRef2++;”

  • a == 28; //true
  • aRef == 28; //true
  • aRef2 == 28; //true
  • aPt[0] == 28; //true
  • *aPt == 28; //true
  • aCopy == 27; //true

McQuain 2012, Gusukuma 2015

slide-8
SLIDE 8

CS2304: C++ for Java Programmers

Pointers n’ stuff (cont)

//declaration of an array int foo[5] = {16,2,77,40,8};

int* fooPt = foo;//WHAAATTT?! int b = fooPt[2]; //What’s b’s value? int& bRef = fooPt[2]; bRef = 36;//What’s the contents of foo?

  • Arrays are pointers allocated on

stack memory (slight lie, but 95% true)

  • (the other 5%)

http://www.geeksforgeeks.org/g- fact-5/

  • References can be made to array

elements

McQuain 2012, Gusukuma 2015

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

CS2304: C++ for Java Programmers

Passing Streams to Functions

  • If you pass a stream to a function, you MUST pass it by reference
  • Passing a stream by value results in undefined behavior

McQuain 2012, Gusukuma 2015

void foo(ifstream input); // wrong void foo(ifstream& input); // right

slide-14
SLIDE 14

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
  • bject

McQuain 2012, Gusukuma 2015

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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