Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ ›...
Transcript of Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ ›...
![Page 1: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/1.jpg)
Programming Abstractions
Cynthia Lee
C S 106B
![Page 2: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/2.jpg)
Topics:
Last week:
› Making your own class
› Arrays in C++
› new/delete
This week: Memory and Pointers
› First revisit some topics from last week in more detail:
• Deeper look at new/delete dynamic memory allocation
• Deeper look at what a pointer is
› Then new topics:
• Linked nodes
• Linked List data structure
• (if we have time) Binary tree data structure
Hat tip to Victoria Kirst of Google for some of today’s slides!
2
![Page 3: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/3.jpg)
new and delete(revisit from last week)
![Page 4: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/4.jpg)
Arrays
type* name = new type[length];
› A dynamically allocated array.
› The variable that refers to the array is a pointer.
› The memory allocated for the array must be manually released,
or else the program will have a memory leak. (>_<)
delete[] name;
› Manually releases the memory back to the computer.
![Page 5: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/5.jpg)
Memory on the stack and heap
void myFunction() {
int x = 5;
int y = 3;
int *heapArr = new int[2];
heapArr[0] = randomInteger(0,3);
// bad -- memory leak coming!
}
Memory
main()
x:
y:
0
myFunction()
heapArr:
![Page 6: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/6.jpg)
Memory on the stack and heapvoid myFunction() {
int x = 5;int y = 3;int *heapArr = new int[2];heapArr[0] = randomInteger(0,3);// bad -- memory leak coming!
}
void randomInteger(int low, int high) {int var1 = 5;double var2 = 3.14159;
…
What happens when myFunction() andrandomInteger() return?
Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)?
Memory
main()
x:
y:
0
myFunction()
heapArr:
v1:
v2:
randomInteger()
![Page 7: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/7.jpg)
Memory on the stack and heapvoid myFunction() {
int x = 5;int y = 3;int *heapArr = new int[2];heapArr[0] = randomInteger(0,3);// bad -- memory leak coming!
}
void randomInteger(int low, int high) {int var1 = 5;double var2 = 3.14159;
…
What happens when myFunction() andrandomInteger() return?
Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)?
Memory
main()
x:
y:
0
myFunction()
heapArr:
randomInteger’s stack
frame automatically
released
![Page 8: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/8.jpg)
Memory on the stack and heapvoid myFunction() {
int x = 5;int y = 3;int *heapArr = new int[2];heapArr[0] = randomInteger(0,3);// bad -- memory leak coming!
}
void randomInteger(int low, int high) {int var1 = 5;double var2 = 3.14159;
…
What happens when myFunction() andrandomInteger() return?
Why do we need to delete heapArr, but not the other variables (x, y, v1, v2)?
Memory
main()
0
myFunction’s stack
frame automatically
released
![Page 9: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/9.jpg)
Always a pair: new and delete
Sample codes from Friday:
// a simple mainint main() {
int* a = new int[3];a[0] = 42;a[1] = -5;a[2] = 17;delete[] a;return 0;
}
// constructor and destructor // in ArrayList.cpp
ArrayList::ArrayList() { myElements = new int[10](); mySize = 0; myCapacity = 10;
}
void ArrayList::~ArrayList() {delete[] myElements;
}
![Page 10: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/10.jpg)
What is a pointer?
![Page 11: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/11.jpg)
Anything wrong with this struct?
struct Album {string title;int year;
string artist_name;int artist_age;string artist_favorite_food;int artist_height; // in cm
};
![Page 12: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/12.jpg)
Anything wrong with this struct?
struct Album {string title;int year;
string artist_name;int artist_age;string artist_favorite_food;int artist_height; // in cm
};
Style-wise seems awkward - "artist_" prefix on fields
Anything else? How many times do you construct the artist info?
![Page 13: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/13.jpg)
Album lemonade = {"Lemonade",2016,"Beyonce",34,"Red Lobster",169
};
Album bday = {"B'Day",2006,"Beyonce",34,"Red Lobster",169
};
Redudant code to declare an
initialize these two album
variables, lemonade and bday
![Page 14: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/14.jpg)
It's redundantly stored, too
lemonade
"B'Day",2006,"Beyonce",34,"Red Lobster",169
"Lemonade",2016,"Beyonce",34,"Red Lobster",169
bday
Storage in memory is also redundant
![Page 15: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/15.jpg)
How do we fix this?
struct Album {string title;int year;
string artist_name;int artist_age;string artist_favorite_food;int artist_height; // in cm
};
Should probably be
another struct?
![Page 16: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/16.jpg)
Does this fix the redundancy?
Artist britney = { "Britney Spears", 34, "Snickers", 163};
Album blackout = { "Blackout", 2007, britney };Album circus = { "Circus", 2008, britney };Album femme_fatale = { "Femme Fatale", 2011, britney };
What does this look like in memory?
struct Artist {string name;int age;string favorite_food;int height; // in cm
};
struct Album {string title;int year;Artist artist;
};
![Page 17: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/17.jpg)
What does it mean when you have a struct field?
struct Artist {string name;int age;string favorite_food;int height; // in cm
};
struct Album {string title;int year;Artist artist;
};
This embeds all the
fields of the Artiststruct into the Albumstruct.
![Page 18: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/18.jpg)
Still stored redundantly
blackout
{"Blackout",2007,{"Britney Spears",34,"Snickers",163
}}
circus
{"Circus",2008,{
"Britney Spears",34,"Snickers",163
}}
britney
"Britney Spears",34,"Snickers",163
Artist britney = { "Britney Spears", 34, "Snickers", 163};Album blackout = { "Blackout", 2007, britney };Album circus = { "Circus", 2008, britney };
![Page 19: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/19.jpg)
Still stored redundantly
blackout
{"Blackout",2007,{"Britney Spears",34,"Snickers",163
}}
circus
{"Circus",2008,{
"Britney Spears",34,"Snickers",163
}}
britney
"Britney Spears",34,"Snickers",163
Artist britney = { "Britney Spears", 34, "Snickers", 163};Album blackout = { "Blackout", 2007, britney };Album circus = { "Circus", 2008, britney };
All the fields of britney are
copied in this step!
![Page 20: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/20.jpg)
Still stored redundantly
blackout
{"Blackout",2007,{"Britney Spears",34,"Snickers",163
}}
circus
{"Circus",2008,{
"Britney Spears",34,"Snickers",163
}}
Artist britney = { "Britney Spears", 34, "Snickers", 163};Album blackout = { "Blackout", 2007, britney };Album circus = { "Circus", 2008, britney };
britney.favorite_food = "Twix";
What happens to the data?
britney
"Britney Spears",34,"Snickers",163
(a) All 3 Snickers change to Twix (b) only britney Snickers changes to Twix
(c) only blackout/circus Snickers changes to Twix
![Page 21: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/21.jpg)
What do we really want?
blackout
title: "Blackout",year: 2007,artist: Please see the
"britney" object
circus
britney
"Britney Spears",age: 34,food: "Snickers",height: 163
The album's artist field should point to the "britney" data structure
instead of storing it.
How do we do this in C++?
…pointers!
title: "Circus",year: 2008,artist: Please see the
"britney" object
![Page 22: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/22.jpg)
new with objects
Example:
Album* album = new Album;album->title = "Blackout";album->year = 2007;
album 0x64 0x24
0x64234123523
340293023
stack
heap"Blackout"
2007
![Page 23: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/23.jpg)
PointersTaking a deeper look at the syntax of that array on the heap
![Page 24: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/24.jpg)
Memory is a giant array
0
38252
4
93402
8
4402
12
5552
16
1952 2042552
24
683
28
82391
32
23532
36
93042
40
bool kitkat = true;int candies = 10;
Whenever you declare a
variable, you allocate a
bucket (or more) of memory
for the value of that variable
Each bucket of memory has
a unique address
50123
![Page 25: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/25.jpg)
Memory addresses
cout << &candies << endl; // 20cout << &kitkat << endl; // 0
Whenever you declare a variable, you
allocate a bucket (or more) of memory
for the value of that variable
Each bucket of memory has a unique
address
You can get the value of a variable's
address using the & operator.
0
38252
4
93402
8
4402
12
5552
16
1952 2042552
24
683
28
82391
32
23532
36
93042
4050123
![Page 26: Programming Abstractionsweb.stanford.edu/class/archive/cs/cs106b/cs106b... · › Arrays in C++ › new/delete This week: Memory and Pointers › First revisit some topics from last](https://reader033.fdocuments.in/reader033/viewer/2022060802/60867a00a84b414890239c72/html5/thumbnails/26.jpg)
Memory addresses
cout << &candies << endl; // 20cout << &kitkat << endl; // 0int* ptrC = &candies; // 20bool* ptrB = &kitkat; // 0
You can store memory addresses in a
special type of variable called a
pointer. i.e. A pointer is a variable that holds a
memory address.
You can declare a pointer by writing
(The type of data it points at)* e.g. int*, string*
0
38252
4
93402
8
4402
12
5552
16
1952 2042552
24
683
28
82391
32
23532
36
93042
4050123