the superhero struct wonderwoman (int) isis (superhero *) superman (int *[]) i use short internal...

44
The superhero struct wonderwoman (int) isis (superhero *) superman (int *[]) I use short internal arrows to remind myself which elements are uninitialized pointers. If a pointer is set to null, you should represent it with a line through the box, like so:

Upload: brenda-adams

Post on 21-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

The superhero struct

wonderwoman (int)

isis (superhero *)

superman (int *[])

I use short internal arrows to remind myself which elements are uninitialized pointers. If a pointer is set to null, you should represent it with a line through the box, like so:

superhero marineboy[2];

superhero marineboy[2];

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

superhero *ironman;

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

superhero *ironman;

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

ironman = &marineboy[1];

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

ironman = &marineboy[1];

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

marineboy[0].wonderwoman = 152;

wonderwoman (int)

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

marineboy[0].wonderwoman = 152;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

marineboy[0].superman[0] = new int[2];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy

ironman

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[0].superman[0] = new int[2];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[0].superman[1] = &(ironman->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[0].superman[1] = &(ironman->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[0].superman[1] = &(ironman->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[0].superman[1] = &(ironman->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

ironman->superman[0] = marineboy[0].superman[1];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

ironman->superman[0] = marineboy[0].superman[1];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

ironman->superman[1] = &(marineboy[0].superman[0][1];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

ironman->superman[1] = &(marineboy[0].superman[0][1]);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

*(ironman->superman[1]) = 9189;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

wonderwoman (int)

isis (superhero *)

superman (int *[])

*(ironman->superman[1]) = 9189;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[1].isis = ironman->isis = ironman;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[1].isis = ironman->isis = ironman;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[1].isis = ironman->isis = ironman;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

marineboy[1].isis = ironman->isis = ironman;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

State before Barberella:

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

stack heap

9189

wonderwoman (int)

isis (superhero *)

superman (int *[])

In Barbarella:

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

We don’t have direct access to electra() local variables anymore. At least not by those names!

wonderwoman (int)

isis (superhero *)

superman (int *[])

barbarella(marineboy[1], ironman->isis);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

These arguments are passed into barbarella, and received on the other side as….

barbarella() stack

wonderwoman (int)

isis (superhero *)

superman (int *[])

barbarella(superhero& storm, superhero *& catwoman)

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

These arguments are passed into barbarella, and received on the other side as….

References!barbarella() stack

storm catwoman

Careful here, catwoman is a REFERENCE to a POINTER, to a superhero

wonderwoman (int)

isis (superhero *)

superman (int *[])

storm.wonderwoman = 465;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

465

isis (superhero *)

superman (int *[])

storm.wonderwoman = 465;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

465

isis (superhero *)

superman (int *[])

catwoman->isis = &storm;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

465

isis (superhero *)

superman (int *[])

catwoman->isis = &storm;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

No change! catwoman->isis is the same as catwoman, which is already pointing at the address of the object that storm references.

wonderwoman (int)

465

isis (superhero *)

superman (int *[])

catwoman->wonderwoman = 830;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman->wonderwoman = 830;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman->isis[0].superman[1] = &(storm.isis->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman->isis[0].superman[1] = &(storm.isis->wonderwoman);

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

Remember: isis[0] is the same as *isis.

Fun fact: isis[-1] is the same as marineboy[0]!That’s because isis[-1] is the same as *(isis + (-1 * sizeOfSuperhero)), or “dereference the address 1 superhero behind isis”

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman = &storm;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman = &storm;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

Another no change operation!

wonderwoman (int)

830

isis (superhero *)

superman (int *[])

catwoman->wonderwoman = 507;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

507

isis (superhero *)

superman (int *[])

catwoman->wonderwoman = 507;

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

507

isis (superhero *)

superman (int *[])

catwoman->isis = new superhero[2];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

507

isis (superhero *)

superman (int *[])

catwoman->isis = new superhero[2];

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman

wonderwoman (int)

507

isis (superhero *)

superman (int *[])

State before barbarella exits:

wonderwoman (int)

152

isis (superhero *)

superman (int *[])

marineboy

ironman

elektra() stack heap

9189

barbarella() stack

storm catwoman