CS 225 Data Structures Wad ade Fag agen-Ulm lmschneid ider - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Wad ade Fag agen-Ulm lmschneid ider - - PowerPoint PPT Presentation

CS 225 Data Structures Wad ade Fag agen-Ulm lmschneid ider #include "sphere.h" puzzle.cpp 1 2 using namespace cs225; Location Value Type Name 3 0xffff00f0 4 Sphere *CreateUnitSphere() { 5 Sphere s(1); 0xffff00e8 6


slide-1
SLIDE 1

CS 225

Data Structures

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

slide-3
SLIDE 3

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

Sphere * s main’s stack frame

slide-4
SLIDE 4

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

Sphere * s main’s stack frame CreateUnitSphere frame Sphere s

slide-5
SLIDE 5

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

Sphere * s main’s stack frame CreateUnitSphere frame Sphere s

slide-6
SLIDE 6

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

Sphere * s main’s stack frame

slide-7
SLIDE 7

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include "sphere.h" using namespace cs225; Sphere *CreateUnitSphere() { Sphere s(1); return &s; } int main() { Sphere *s = CreateUnitSphere(); someOtherFunction(); double r = s->getRadius(); double v = s->getVolume(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

puzzle.cpp

Sphere * s main’s stack frame someOtherFunction

slide-8
SLIDE 8

Heap Memory ry

0x42000 0x42008 0x42010 0x42018 0x42020 0x42028 0x42030 0x42038 0x42040 0x42048

slide-9
SLIDE 9

Heap Memory - new

As programmers, we can use heap memory in cases where the lifecycle of the variable exceeds the lifecycle of the function. The only way to create heap memory is with the use of the new keyword. Using new will: 1. 2. 3.

slide-10
SLIDE 10

Heap Memory - delete

  • 2. The only way to free heap memory is with the use of the

delete keyword. Using delete will:

  • 3. Memory is never automatically reclaimed, even if it goes
  • ut of scope. Any memory lost, but not freed, is

considered to be “leaked memory”.

slide-11
SLIDE 11

Heap Memory vs. . Stack Memory Lifecycle

slide-12
SLIDE 12

0x42000 0x42008 0x42010 0x42018 0x42020 0x42028 0x42030 0x42038 0x42040 0x42048

#include "sphere.h" using namespace cs225; int main() { int *p = new int; int *s = new Sphere(10); return 0; } 1 2 3 4 5 6 7 8 9 10 11

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0

Location Value Type Name

heap1.cpp

slide-13
SLIDE 13

0x42000 0x42008 0x42010 0x42018 0x42020 0x42028 0x42030 0x42038 0x42040 0x42048

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0

#include "sphere.h" using namespace cs225; int main() { Sphere *s1 = new Sphere(); Sphere *s2 = s1; s2->setRadius( 10 ); return 0; } 1 2 3 4 5 6 7 8 9 10 11

heap2.cpp

slide-14
SLIDE 14

Exam 1 Topics

slide-15
SLIDE 15

MP1

slide-16
SLIDE 16

#include <iostream> using namespace std; int main() { int i = 2, j = 4, k = 8; int *p = &i, *q = &j, *r = &k; k = i; cout << i << j << k << *p << *q << *r << endl; p = q; cout << i << j << k << *p << *q << *r << endl; *q = *r; cout << i << j << k << *p << *q << *r << endl; }

copy.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

slide-17
SLIDE 17

#include <iostream> using namespace std; int main() { int *x = new int; int &y = *x; y = 4; cout << &x << endl; cout << x << endl; cout << *x << endl; cout << &y << endl; cout << y << endl; cout << *y << endl; }

heap-puzzle1.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

slide-18
SLIDE 18

#include <iostream> using namespace std; int main() { int *p, *q; p = new int; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *p << endl; cout << *q << endl; return 0; }

heap-puzzle2.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

slide-19
SLIDE 19

#include <iostream> using namespace std; int main() { int *x; int size = 3; x = new int[size]; for (int i = 0; i < size; i++) { x[i] = i + 3; } delete[] x; }

heap-puzzle3.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

slide-20
SLIDE 20

/* * Creates a new sphere that contains the exact volume * of the two input spheres. */ Sphere joinSpheres(Sphere s1, Sphere s2) { double totalVolume = s1.getVolume() + s2.getVolume(); double newRadius = std::pow( (3.0 * totalVolume) / (4.0 * 3.141592654), 1.0/3.0 ); Sphere result(newRadius); return result; }

joinSpheres.cpp

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

slide-21
SLIDE 21

CS 225 – Things To Be Doing

Register for Exam 1 (CBTF)

Details on the course website!

Every day, work on the POTDs

Available on PrairieLearn, every weekday!

Finish MP1

Due: Monday, Sept. 11th (11:59pm)

Attend lab and complete lab_debug

Due: Sunday, Sept. 10th (11:59pm)