Pointers in C++ (Basically) everything in C++ resides somewhere in - - PowerPoint PPT Presentation

pointers in c basically everything in c resides somewhere
SMART_READER_LITE
LIVE PREVIEW

Pointers in C++ (Basically) everything in C++ resides somewhere in - - PowerPoint PPT Presentation

Pointers in C++ (Basically) everything in C++ resides somewhere in memory int main() { int a = 23; cout << "a's value is " << a; } a has an address But what is it? int main() { int a = 23; cout <<


slide-1
SLIDE 1

Pointers in C++

slide-2
SLIDE 2

(Basically) everything in C++ resides somewhere in memory

slide-3
SLIDE 3

int main() { int a = 23; cout << "a's value is " << a; }

slide-4
SLIDE 4

int main() { int a = 23; cout << "a's value is " << a; } “a” has an address But what is it?

slide-5
SLIDE 5

&a

Give me the address of a

slide-6
SLIDE 6

Please Please Please Don’t get this confused with a reference They are totally different things!

slide-7
SLIDE 7

Please Please Please Don’t get this confused with a reference They are totally different things! (Forget about references for now..)

slide-8
SLIDE 8

Everything in C++ also has a size

slide-9
SLIDE 9

sizeof(a)

slide-10
SLIDE 10

This whole thing is a 0x7fff5388990c 1st 2nd 3rd 4th

slide-11
SLIDE 11

Local variables are stored on the stack Variables next to each other, are placed next to each other on the stack

slide-12
SLIDE 12

So now, what will this do?

slide-13
SLIDE 13

int main() { int a = 23; int b = 24; cout << "a's value is " << a << endl; cout << "a's address is " << &a << endl; cout << "the size of a is " << sizeof(a) << endl; cout << "b's value is " << b << endl; cout << "b's address is " << &b << endl; cout << "the size of b is " << sizeof(b) << endl; }

Assuming a is 0x7fff5388990c

slide-14
SLIDE 14

Lesson: the stack grows down

slide-15
SLIDE 15

When C++ calls a function, it creates space for its local variables on the stack When C++ returns from a function, it destroys those by moving the stack up

slide-16
SLIDE 16

Let’s call another…

slide-17
SLIDE 17

If I know someone’s address, I can go get the data at that address…

slide-18
SLIDE 18

*(&a) is the same as a

slide-19
SLIDE 19

I can use the * operator to get the data at some address

slide-20
SLIDE 20

I can even store addresses int *pointerToA = &a;

slide-21
SLIDE 21

void storingPtr() { int a = 23; int *pointerToA = &a; cout << "a's value is " << *pointerToA << endl; cout << "&a is " << &a << endl; cout << "pointerToA is " << pointerToA << endl; cout << "&pointerToA is " << &pointerToA << endl; cout << "sizeof(pointerToA) is " << sizeof(pointerToA) << endl; return; }

slide-22
SLIDE 22

Note: all pointers take up the same number of bytes And that number depends on your machine (32/64-bit)

slide-23
SLIDE 23

What happens if I want to use a pointer after the function returns?

slide-24
SLIDE 24

int *returnsABadPointer() { int a = 23; int *ptr = &a; cout << "the value of *ptr is " << *ptr << endl; return &a; } void doSomethingBad() { int *ptr = returnsABadPointer(); cout << "the value of *ptr is " << *ptr << endl; return; }

slide-25
SLIDE 25

Lesson: once the function returns, that pointer is meaningless

slide-26
SLIDE 26

Lesson: once the function returns, that pointer is meaningless Even worse, if I continue to use it, it could cause security problems

slide-27
SLIDE 27

Lesson: once the function returns, that pointer is meaningless Even worse, if I continue to use it, it could cause security problems An attacker could figure out how to load their data into *ptr and control my code

slide-28
SLIDE 28

So how can I hold onto things after returns!?

slide-29
SLIDE 29
slide-30
SLIDE 30

I use the heap

slide-31
SLIDE 31

Unlike the stack, when I put things on the heap, they stay there until I tell them to go away

slide-32
SLIDE 32

int *newPointer() { int *a = new int; *a = 23; return a; } void doSomethingFine() { int *ptr = newPointer(); cout << "the value of *ptr is " << *ptr; *ptr++ cout << "the value of *ptr is " << *ptr; delete ptr; }

slide-33
SLIDE 33

int *newPointer() { int *a = new int; *a = 23; return a; } void doSomethingFine() { int *ptr = newPointer(); cout << "the value of *ptr is " << *ptr; *ptr++ cout << "the value of *ptr is " << *ptr; delete ptr; }

Note: I used delete

slide-34
SLIDE 34

If I don’t remember to call delete, the memory will never go away It will live on forever, like a zombie Gradually, the world will be taken over…

slide-35
SLIDE 35
slide-36
SLIDE 36

void useAllMyMemory() { for (long long i = 0; i < 12346789000; i++) { int *ptr = new int; } return; }

slide-37
SLIDE 37

void dontUseAllMyMemory() { for (long long i = 0; i < 12346789000; i++) { int *ptr = new int; delete ptr; } return; }

slide-38
SLIDE 38