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

Lecture notes from a CS 106B lecture at Stanford University. The lecture covers dynamic memory allocation in C++, including pointers, structs, the -> operator, and the new and delete keywords. The notes also include a mystery function example with output. The document could be useful as study notes or exam preparation for a computer science course covering C++ and memory allocation.

Typology: Lecture notes

2017/2018

Uploaded on 05/11/2023

jamal33
jamal33 šŸ‡ŗšŸ‡ø

4.3

(51)

340 documents

1 / 62

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e

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