C++ Memory Pointers and joy! January 07, 2019 Cinda Heeren / Will - - PowerPoint PPT Presentation

c memory
SMART_READER_LITE
LIVE PREVIEW

C++ Memory Pointers and joy! January 07, 2019 Cinda Heeren / Will - - PowerPoint PPT Presentation

C++ Memory Pointers and joy! January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 Announcements HW1 corrections please check pinned Piazza post Gradescope registrations will start today check your ugrad e-mail for


slide-1
SLIDE 1

C++ Memory

Pointers and joy!

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • HW1 corrections

– please check pinned Piazza post – Gradescope registrations will start today – check your ugrad e-mail for invitation

  • Cinda's C++ mini-camps happening this week!

– Monday, Tuesday, Wednesday, 18:00-20:00, SWNG 122, 221, 122

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2

slide-3
SLIDE 3

Code analysis

  • The loop variables here does not increment in the usual way

– Then how many times is the loop body executed?

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3

Loop variables

void candyapple(int n) { for (int i = 1; i < n; i *= 3) cout << "iteration: " << i << endl; } void caramelcorn(int n) { for (int i = 0; i * i < 6 * n; i++) cout << "iteration: " << i << endl; }

slide-4
SLIDE 4

A visual aid for loop executions

  • Determine the range of your loop variable
  • Determine how many elements within that range will be "hit"
  • Complexities of nested loops are (usually) multiplied
  • Complexities of separate loops are (usually) added

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4

int i, j; for (i = 1; i < 9*n; i = i*2) { for (j = n*n; j > 0; j--) { ... } } 1 9𝑜 1 𝑜2

slide-5
SLIDE 5

Memory

Functions Dynamic memory and... Pointers!

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5

slide-6
SLIDE 6

Calling functions in C++

  • Actual parameter

– Value(s) or variable(s) specified by the function caller

  • Formal parameter

– Variables found in the signature/header of the function itself

  • Formal parameters must match with actual parameters in
  • rder, number, and data type

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6

Parameters

int a = 5; int b = 7; int result = sum(a, b); int sum(int x, int y) { return x + y; } Actual parameters Formal parameters

slide-7
SLIDE 7

Function parameters

  • Unless written otherwise, (most) parameters in C++ are

passed by value ("call-by-value")

– the value of the actual parameter is copied to the formal parameter when the function is called

  • The actual parameters and formal parameters are different

variables in memory, even if they are named the same

  • If you change the value of the formal parameter, this does not

affect the value of the actual parameter back in the caller's memory

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 7

slide-8
SLIDE 8

Function parameters and the call stack

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8

Example

// ... int r = 3; double area = circleArea(r); // ... double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory

3

r

3.0

radius

3.1415

pi

3.0

x

slide-9
SLIDE 9

Function parameters and the call stack

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9

Example

// ... int r = 3; double area = circleArea(r); // ... double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory

3

r

3.0

radius

3.1415

pi

9.0

sq_r

slide-10
SLIDE 10

Function parameters and the call stack

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10

Example

// ... int r = 3; double area = circleArea(r); // ... double circleArea(double radius){ double pi = 3.1415; double sq_r = square(radius); return sq_r * pi; } double square(double x){ return x * x; } main memory

3

r

28.274

area

slide-11
SLIDE 11

Call-by-value problems

  • Do the values really get swapped in the calling function?

– What does the call stack look like?

  • This can be fixed using call-by-reference

– add a '&' symbol after the parameter's data type in the signature – see mystery function from Jan.04 slides

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11

int a = 5; int b = 7; swap(a, b); cout << "a: " << a << endl; cout << "b: " << b << endl; void swap(int x, int y) { int temp = x; x = y; y = temp; }

slide-12
SLIDE 12

Dynamic memory

  • Variables declared in a function only exist within the scope of

that function (or code block enclosed by { } )

  • The data structures we will learn about in this course require
  • bjects and variables to persist beyond a function's lifetime

– cannot be allocated to call stack, need to put somewhere else – heap memory / dynamic memory

  • We still need local variables that refer or point to the

dynamically allocated memory

– In C++ such variables are pointers

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 12

aka heap memory

slide-13
SLIDE 13

Addresses and pointers

  • Every storage location in memory (RAM) has an address

associated with it

– The address is the location in memory where a given variable or identifier stores its data

  • Can think of address in memory like a mailbox number

– Use the address to find where the mailbox is – Look inside the mailbox to access the contents/value

  • A pointer is a special type of variable

– That stores an address rather than a value – The address is used to find a value elsewhere in memory

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13

slide-14
SLIDE 14

Declaring pointers

  • Pointer variables are declared as follows:

datatype* identifier – e.g. int* ptr; or int * ptr; or int *ptr;

  • Note that the type of a pointer is not the same as the type it

points to

– e.g. ptr is a pointer to an int, but is itself not an int

  • Warning! The declaration

int* var1, var2; – declares var1 as a pointer, but var2 as an integer!

  • To declare both as pointers, either declare individually, or:

int *var1, *var2;

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 14

slide-15
SLIDE 15

Address operator and dereferencing

  • Pointers can be assigned the address of an existing variable

– Using the address operator, &

  • The value which a pointer points to can be accessed by

dereferencing the pointer

– Using the * operator

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 15

… 1 220-1 int x = 23; 23 212 x int* p = &x; 4096 … p x = 47; 47 *p = 38; 38

slide-16
SLIDE 16

Pointers as parameters

  • Passing pointer parameters allows the function to access

and/or modify the actual parameter

int getArraySum(vector<int>& arr, int* pcount) { int sum = 0; for (int i = 0; i < arr.size(); i++) { if (arr[i] > 0) (*pcount)++; sum += arr[i]; } return sum; } int numpositive = 0; vector<int> numbers; // assume that 3, 7, -9, 5, -4 get push_back-ed into numbers int result = getArraySum(numbers, &numpositive); cout << "Array sum: " << result << endl; cout << "Number of positive elements: " << numpositive << endl;

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16

slide-17
SLIDE 17

Pointers as parameters

Another example

void f1(int arg) { arg = 22; cout << "f1 arg: " << arg << "\n"; } void f2(int* arg) { *arg = 410; cout << "f2 arg: " << arg << "\n"; } int x = 45; f1(x); cout << "x after f1: " << x << "\n"; f2(&x); cout << "x after f2: << x << "\n";

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17

slide-18
SLIDE 18

Pointer to a pointer

  • "You can keep adding levels of pointers until your brain

explodes or the compiler melts – whichever happens soonest"

– stackoverflow user JeremyP

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 18

...to a pointer to a pointer...

int main() { int x = 5; int* p = &x; *p = 6; int** q = &p; int*** r = &q; cout << "*p: " << *p << endl; cout << "*q: " << *q << endl; cout << "**q: " << *(*q) << endl; }

slide-19
SLIDE 19

Pointers and dynamic memory

  • The new keyword allocates space in dynamic memory and

returns the first address of the allocated space

  • delete releases the memory at the address referenced by its

pointer variable

– delete[] is used to release memory allocated to array variables Beware of memory leaks!

int a = 5; int* b = new int; int* c = &a; *c = 4; int** d = &b; int* e = new int[a]; **d = 3; int* f = new int[*b]; delete b; delete e; // causes a memory leak delete[] f;

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 19

allocates an array of size a in dynamic memory

slide-20
SLIDE 20

Readings for this lesson

  • Carrano & Henry

– Background: 1.4, C1.1-C1.4 – C2.3, C2.5 (Pointers, dynamic arrays)

  • Next class

– Carrano & Henry, Chapter 4 (Linked lists)

January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 20