powerpoint presentation · stack memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0...

23
CS 225 Data Structures

Upload: others

Post on 29-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

CS 225 Data Structures

Page 2: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#ifndef SPHERE_H

#define SPHERE_H

namespace cs225 {

class Sphere {

public:

Sphere();

Sphere(double r);

double getRadius();

double getVolume();

private:

double r_;

};

}

#endif

sphere.h 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include "sphere.h"

namespace cs225 {

Sphere::Sphere() {

}

Sphere::Sphere(double r) {

r_ = r;

// …lots of other logic…

}

double Sphere::getRadius() {

return r_;

}

double Sphere::getVolume() {

return (4 * r_ * r_ * r_ *

3.14159265) / 3.0;

}

}

sphere.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Page 3: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Stack Memory 0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

Page 4: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Location Value Type Name int main() {

int a;

int b = -3;

int c = 12345;

int *p = &b;

return 0;

}

1 2

3

4

5

6

7

8

9

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

example1.cpp

Page 5: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include <iostream>

int main() {

std::cout << sizeof(int) << std::endl;

return 0;

}

sizeof-int.cpp 1 2

3

4

5

6

Page 6: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include <iostream>

int main() {

std::cout << sizeof(int *) << std::endl;

return 0;

}

sizeof-intptr.cpp 1 2

3

4

5

6

Page 7: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Location Value Type Name int main() {

int a;

int b = -3;

int c = 12345;

int *p = &b;

return 0;

}

1 2

3

4

5

6

7

8

9

&a: 0x7ffe2ee87218

&b: 0x7ffe2ee87214

&c: 0x7ffe2ee87210

&p: 0x7ffe2ee87208

Real results when running on linus.ews.illinois.edu

0x7ffe2ee87228

0x7ffe2ee87220

0x7ffe2ee87218

0x7ffe2ee87210

0x7ffe2ee87208

0x7ffe2ee87200

0x7ffe2ee871f8

0x7ffe2ee871f0

0x7ffe2ee871e8

0x7ffe2ee871e0

example1.cpp

Page 8: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Location Value Type Name #include "sphere.h"

int main() {

cs225::Sphere s;

cs225::Sphere *p = &s;

return 0;

}

1 2

3

4

5

6

7

8

9

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

example2.cpp

Page 9: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include <iostream>

#include "sphere.h"

int main() {

std::cout << sizeof(cs225::Sphere) << std::endl;

std::cout << sizeof(cs225::Sphere *) << std::endl;

return 0;

}

sizeof-sphere.cpp 1 2

3

4

5

6

7

8

Page 10: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Stack Frames int hello() {

int a = 100;

return a;

}

int main() {

int a;

int b = -3;

int c = hello();

int d = 42;

return 0;

}

1 2

3

4

5

6

7

8

9

10

11

12

13

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

stackframe.cpp

Page 11: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Return Values

Page 12: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Location Value Type Name #include "sphere.h"

using namespace cs225;

Sphere *CreateUnitSphere() {

Sphere s(1);

return &s;

}

int main() {

Sphere *s = CreateUnitSphere();

double r = s->getRadius();

double v = s->getVolume();

return 0;

}

1 2

3

4

5

6

7

8

9

10

11

12

13

14

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

sphere-badCreate.cpp

Page 13: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

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();

double r = s->getRadius();

double v = s->getVolume();

return 0;

}

1 2

3

4

5

6

7

8

9

10

11

12

13

14

sphere-badCreate.cpp

Page 14: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Location Value Type Name

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffee00f0

0xffee00e8

0xffee00e0

0xffee00d8

0xffee00d0

#include "sphere.h"

using namespace cs225;

Sphere *CreateUnitSphere() {

Sphere s(1);

return &s;

}

int main() {

Sphere *s = CreateUnitSphere();

double r = s->getRadius();

double v = s->getVolume();

return 0;

}

1 2

3

4

5

6

7

8

9

10

11

12

13

14

sphere-badCreate.cpp

Page 15: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

What happens on a real system?

Page 16: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

0xffee00f0

0xffee00e8

0xffee00e0

0xffee00d8

0xffee00d0

&s (CreateUnitSphere): 0x7ffee6bf5ca8

1

s->getRadius(): 2.07941e-317

&s (main): 0x7ffee6bf5d30

s (main): 0x7ffee6bf5ca8

&r (main): 0x7ffee6bf5d28

r (main): 6.95312e-310

&v (main): 0x7ffee6bf5d20

v (main): 0

Real results when running on linus.ews.illinois.edu

0x7ffee6bf5d38

0x7ffee6bf5d30

0x7ffee6bf5d28

0x7ffee6bf5d20

0x7ffee6bf5d18

int main() {

Sphere *s = CreateUnitSphere();

cout << s->getRadius() << endl;

cout << "s->getRadius(): “

<< s->getRadius() << endl;

cout << "&s (main): " << &s << endl;

cout << " s (main): " << s << endl;

double r = s->getRadius();

cout << "&r (main): " << &r << endl;

cout << " r (main): " << r << endl;

double v = s->getVolume();

cout << "&v (main): " << &v << endl;

cout << " v (main): " << v << endl;

}

13

14

15

16

17

18

19

20

21

22

23

24

25

0x7ffee6bf5cb0

0x7ffee6bf5ca8

0x7ffee6bf5ca0

0x7ffee6bf5c98

0x7ffee6bf5c90

(0x60 bytes not shown)

Page 17: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Stack Memory vs. Heap Memory

0x42000

0x42008

0x42010

0x42018

0x42020

0x42028

0x42030

0x42038

0x42040

0x42048 0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

0xffff00c8

0xffff00c0

0xffff00b8

0xffff00b0

0xffff00a8

Page 18: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

Heap Memory

0x42000

0x42008

0x42010

0x42018

0x42020

0x42028

0x42030

0x42038

0x42040

0x42048

Page 19: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

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

Location Value Type Name

0xffff00f0

0xffff00e8

0xffff00e0

0xffff00d8

0xffff00d0

Location Value Type Name

heap1.cpp

Page 20: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

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

Page 21: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#include <iostream>

using namespace std;

int main() {

int *p;

int x;

p = &x;

x = 6;

cout << x << endl;

cout << p << endl;

return 0;

}

extra-puzzle1.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

Page 22: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

#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;

}

extra-puzzle2.cpp 1 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Page 23: PowerPoint Presentation · Stack Memory 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

CS 225 – Things To Be Doing lab_intro Due Sunday by 11:59pm Make sure to turn in lab_intro by Sunday at 11:59pm

MP1 is out tonight! (after labs have finished) Due: Monday, Sept. 11th (weekend + week + weekend from now)

Office Hours are Starting Up… ~20 hours of office hours this weekend (Check “Calendar” on CS 225 website) - Having trouble with C++? - Having trouble with lab_intro? - Don’t understand something from lecture?

Have an awesome holiday weekend! …see you next Wednesday!