pointers in c basically everything in c resides somewhere
play

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


  1. Pointers in C++

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

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

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

  5. &a Give me the address of a

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

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

  8. Everything in C++ also has a size

  9. sizeof(a)

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

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

  12. So now, what will this do?

  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

  14. Lesson: the stack grows down

  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

  16. Let’s call another…

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

  18. *(&a) is the same as a

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

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

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

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

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

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

  25. Lesson: once the function returns, that pointer is meaningless

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

  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

  28. So how can I hold onto things after returns!?

  29. I use the heap

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

  31. 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; }

  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; } Note: I used delete

  33. 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…

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend