Docsity

CS 106B Lecture 15: Dynamic Memory Allocation, Lecture notes of Operating Systems

Partial preview of the text

Download CS 106B Lecture 15: Dynamic Memory Allocation and more Lecture notes Operating Systems in PDF only on Docsity!

Friday, May 4, 2018

Programming Abstractions Spring 2018 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming Abstractions in C++, Chapter 11 CS 106B Lecture 15: Dynamic Memory Allocation

Today's Topics

  • Logistics
    • Question from a student: "Is there any other interesting youtube channel you can

recommend besides Numberphile?" Glad you asked! (see YouTube Video page at

the end of the slides)

  • More on Pointers
    • Mystery function
    • Pointers and Structs
    • The -> operator
  • Dynamic Memory Allocation
    • The new and delete keywords
    • The "heap"

Introduction to Pointers

What is a pointer??

a memory address!

Mystery Function: What prints out? void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Mystery Function: What prints out? void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Answer: a 4 0x b 8 0xab c

0xf a

0x5e b //// /////// c

0x7c

Mystery Function: What prints out? void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Answer: a 4 0x b 8 0xab c

0xf a

0x5e b //// /////// c

0x7c

Mystery Function: What prints out? 4 8 - Answer: a 4 0x b 7 0xab c

0xf a

0x5e b //// /////// c

0x7c void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Mystery Function: What prints out? 4 8 - Answer: a 4 0x b 7 0xab c

0xf a

0x5e b //// /////// c

0x7c void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Mystery Function: What prints out? 4 8 - Answer: a 11 0x b 7 0xab c

0xf a

0x5e b //// /////// c

0x7c void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Mystery Function: What prints out? 4 8 - -2 11 7 Answer: a 11 0x b 7 0xab c

0xf a

0x5e b //// /////// c

0x7c void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Mystery Function: What prints out? 4 8 - -2 11 7 11 7 - Answer: a 11 0x b 7 0xab c

0xf a

0x5e b //// /////// c

0x7c void mystery(int a, int& b, int* c) { a++; (*c)--; b += *c; cout << a << " " << b << " " << *c << " " << endl; } int main() { int a = 4; int b = 8; int c = -3; cout << a << " " << b << " " << c << " " << endl; mystery(c, a, &b); cout << a << " " << b << " " << c << " " << endl; return 0; }

Pointers and Structs

  • (^) Pointers can point to a struct or class instance as well as to a regular variable.
  • (^) One way to do this would be to dereference and then use dot notation: date 0xa month 10 day 31 daysInMonth() toString() ... ... Date d; d.month = 7; Date dPtr = &d; cout << (dPtr).month << endl;** dPtr 0xa 0x7c

Pointers and Structs

  • (^) So, we have a different, and more intuitive syntax, called the "arrow" syntax, -> : date 0xa month 10 day 31 daysInMonth() toString() ... ... Date d; d.month = 7; Date dPtr = &d; cout << dPtr->month << endl;*
  • (^) We will use the arrow syntax almost exclusively when using structs. dPtr 0xa 0x7c

Pointers and Structs

  • (^) The arrow syntax can be used to set a value or call a function in a struct via a pointer, as well: date 0xa month 10 day 31 daysInMonth() toString() ... ... Date d; d.month = 7; Date dPtr = &d; cout << dPtr->month << endl; p->day = 1; cout << p->day << endl; // 1 cout << p->toString() << endl; // 10/* dPtr 0xa 0x7c