CS 225 Data Structures August 31 Memory Wad ade Fag agen-Ulm - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures August 31 Memory Wad ade Fag agen-Ulm - - PowerPoint PPT Presentation

CS 225 Data Structures August 31 Memory Wad ade Fag agen-Ulm lmschneid ider Pointers and References A variable containing an instance of an object: 1 Cube s1; A reference variable of a Cube object: 1 Cube & s1; A variable


slide-1
SLIDE 1

CS 225

Data Structures

August 31 – Memory

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

Pointers and References

A variable containing an instance of an object: A reference variable of a Cube object: A variable containing a pointer to a Cube object:

1 Cube s1; 1 Cube * s1; 1 Cube & s1;

slide-3
SLIDE 3

Pointers

Three key ideas: 1. 2. 3.

slide-4
SLIDE 4

#include <iostream> #include "Cube.h" int main() { cs225::Cube c; std::cout << "Address storing `c`:" << &c << std::endl; cs225::Cube *ptr = &c; std::cout << "Addr. storing ptr: "<< &ptr << std::endl; std::cout << "Contents of ptr: "<< ptr << std::endl; return 0; }

main.cpp

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

slide-5
SLIDE 5

In Indirection Operators

Given any variable v: &v *v v->

slide-6
SLIDE 6

Stack Memory

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

#include <iostream> int main() { std::cout << sizeof(int) << std::endl; return 0; }

sizeof-int.cpp

1 2 3 4 5 6

slide-9
SLIDE 9

#include <iostream> int main() { std::cout << sizeof(int *) << std::endl; return 0; }

sizeof-intptr.cpp

1 2 3 4 5 6

slide-10
SLIDE 10

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

slide-11
SLIDE 11

Location Value Type Name

#include "Cube.h" int main() { cs225::Cube c; cs225::Cube *p = &c; return 0; } 1 2 3 4 5 6 7 8 9

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

example2.cpp

slide-12
SLIDE 12

#include <iostream> #include "Cube.h" int main() { std::cout << sizeof(cs225::Cube) << std::endl; std::cout << sizeof(cs225::Cube *) << std::endl; return 0; }

sizeof-cube.cpp

1 2 3 4 5 6 7 8

slide-13
SLIDE 13

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

slide-14
SLIDE 14
slide-15
SLIDE 15

Problems of f the Day (P (POTD)

POTDs are small, daily problems for you to practice programming in an environment similar to the CBTF exam environment. Each POTD is worth +1 extra credit point, capped at +40. (Course-wide, all extra credit is capped at +100.) POTD#1 is available on Tuesday, until 8:00am Wednesday morning when POTD#2 becomes available!

slide-16
SLIDE 16
slide-17
SLIDE 17

Location Value Type Name

#include "Cube.h" using cs225::Cube; Cube *CreateCube() { Cube c(20); return &c; } int main() { Cube *c = CreateCube(); double r = c->getVolume(); double v = c->getSurfaceArea(); 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

puzzle.cpp

slide-18
SLIDE 18

Location Value Type Name

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

#include "Cube.h" using cs225::Cube; Cube *CreateCube() { Cube c(20); return &c; } int main() { Cube *c = CreateCube(); double r = c->getVolume(); double v = c->getSurfaceArea(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14

puzzle.cpp

slide-19
SLIDE 19

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffee00f0 0xffee00e8 0xffee00e0 0xffee00d8 0xffee00d0

#include "Cube.h" using cs225::Cube; Cube *CreateCube() { Cube c(20); return &c; } int main() { Cube *c = CreateCube(); double r = c->getVolume(); double v = c->getSurfaceArea(); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14

puzzle.cpp

slide-20
SLIDE 20

What happens on a real system?

slide-21
SLIDE 21

+0x60

int main() { Cube *c = CreateCube(); cout << c-> getVolume() << endl; cout << "c->getVolume(): “ << c->getVolume() << endl; cout << "&c (main): " << &c << endl; cout << " c (main): " << c << endl; double r = c->getVolume(); cout << "&r (main): " << &c << endl; cout << " r (main): " << c << endl; double v = c->getSurfaceArea(); cout << "&v (main): " << &c << endl; cout << " v (main): " << c << endl; return 0;

0xffee00f0 0xffee00e8 0xffee00e0 0xffee00d8 0xffee00d0

&c (CreateCube): 0x7ffee6bf5ca8 8000 c->getVolume(): 2.07941e-317 &c (main): 0x7ffee6bf5d30 c (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 13 14 15 16 17 18 19 20 21 22 23 24 25

0x7ffee6bf5d38 0x7ffee6bf5d30 0x7ffee6bf5d28 0x7ffee6bf5d20 0x7ffee6bf5d18 0x7ffee6bf5cb0 0x7ffee6bf5ca8 0x7ffee6bf5ca0 0x7ffee6bf5c98 0x7ffee6bf5c90

slide-22
SLIDE 22

int main() { Cube *c = CreateCube(); cout << c-> getVolume() << endl; cout << "c->getVolume(): “ << c->getVolume() << endl;

0xffee00f0 0xffee00e8 0xffee00e0 0xffee00d8 0xffee00d0

&c (CreateCube): 0x7ffee6bf5ca8 8000 c->getVolume(): 2.07941e-317 Real results when running on linus.ews.illinois.edu 13 14 15 16 17 18 19 20 21 22 23 24 25

slide-23
SLIDE 23

Stack Memory vs. . H Heap Memory ry

0x42000 0x42008 0x42010 0x42018 0x42020 0x42028 0x42030 0x42038 0x42040 0x42048 0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0 0xffff00c8 0xffff00c0 0xffff00b8 0xffff00b0 0xffff00a8

slide-24
SLIDE 24

Heap Memory ry

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

slide-25
SLIDE 25

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-26
SLIDE 26

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-27
SLIDE 27

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

#include "Cube.h" using cs225::Cube; int main() { int *p = new int; Cube *c = new Cube(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

slide-28
SLIDE 28

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

Location Value Type Name

0xffff00f0 0xffff00e8 0xffff00e0 0xffff00d8 0xffff00d0

#include "Cube.h" using cs225::Cube; int main() { Cube *c1 = new Cube(); Cube *c2 = c1; c2->setLength( 10 ); return 0; } 1 2 3 4 5 6 7 8 9 10 11

heap2.cpp

slide-29
SLIDE 29

#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

slide-30
SLIDE 30

#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