






















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 62
This page cannot be seen from the preview
Don't miss anything!























































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
What is a pointer??
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 and Structs
Pointers and Structs