c memory
play

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


  1. C++ Memory Pointers and joy! January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

  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

  3. Code analysis Loop variables • The loop variables here does not increment in the usual way – Then how many times is the loop body executed? 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; } January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3

  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 int i, j; for (i = 1; i < 9*n; i = i*2) { 1 9𝑜 for (j = n*n; j > 0; j--) { ... } 𝑜 2 1 } January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4

  5. Memory Functions Dynamic memory and... Pointers! January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5

  6. Calling functions in C++ Parameters int a = 5; int sum(int x, int y) { int b = 7; return x + y; int result = sum(a, b); } Formal parameters Actual parameters • 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 order , number , and data type January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6

  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

  8. Function parameters and the call stack Example // ... double circleArea(double radius){ double pi = 3.1415; int r = 3; double area = circleArea(r); double sq_r = square(radius); // ... return sq_r * pi; } double square(double x){ return x * x; } main memory 3 3.0 3.1415 3.0 r pi x radius January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8

  9. Function parameters and the call stack Example // ... double circleArea(double radius){ double pi = 3.1415; int r = 3; double area = circleArea(r); double sq_r = square(radius); // ... return sq_r * pi; } double square(double x){ return x * x; } main memory 3 3.0 3.1415 9.0 r pi sq_r radius January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9

  10. Function parameters and the call stack Example // ... double circleArea(double radius){ double pi = 3.1415; int r = 3; double area = circleArea(r); double sq_r = square(radius); // ... return sq_r * pi; } double square(double x){ return x * x; } main memory 3 28.274 r area January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10

  11. Call-by-value problems int a = 5; void swap(int x, int y) { int b = 7; int temp = x; swap(a, b); x = y; cout << "a: " << a << endl; y = temp; cout << "b: " << b << endl; } • 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

  12. Dynamic memory aka heap 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 objects 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

  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

  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

  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 p x … … 47 38 4096 23 2 12 2 20 -1 0 1 int x = 23; int* p = &x; x = 47; *p = 38; January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 15

  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

  17. Pointers as parameters Another example void f1(int arg) int x = 45; { arg = 22; f1(x); cout << "f1 arg: " cout << "x after f1: " << arg << "\n"; << x << "\n"; } f2(&x); cout << "x after f2: void f2(int* arg) << x << "\n"; { *arg = 410; cout << "f2 arg: " << arg << "\n"; } January 07, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17

  18. Pointer to a pointer ...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; } • "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

  19. Pointers and dynamic memory Beware of memory leaks! • 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 int a = 5; int* b = new int; int* c = &a; allocates an array of size a *c = 4; in dynamic memory 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

  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

Recommend


More recommend