1 / 25

Pointers, Arrays, Destructors

Pointers, Arrays, Destructors. Is this random stuff or are these somehow connected?. Pointers. What is a pointer? a memory address a special data type. Pointers. What do pointers look like? int * pNumber ; MyClass * pObject = NULL; Anything funny about pNumber above?

terah
Download Presentation

Pointers, Arrays, Destructors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

  2. Pointers • What is a pointer? • a memory address • a special data type

  3. Pointers • What do pointers look like? • int* pNumber; • MyClass* pObject = NULL; • Anything funny about pNumber above? • No – it is fine • Yes – it is uninitialized

  4. Pointers • What can I use pointers for? • store the address of a static variable or object • access the variable or object through the pointer int number = 10; int* pNumber = &number; *pNumber = 99; cout << number; What is the output? 99

  5. Pointers • What can I use pointers for? • dynamically allocate single variables or objects string* pStr = new string(“hello”); cout << (*pStr) << “ has “; cout << pStr->length() << “ characters.”; delete pStr; pStr = NULL;

  6. Pointers • What can I use pointers for? • dynamically allocate arrays of variables or objects MyClass* myObjects = new MyClass[10]; myObject[0].setName(“Ziggy”); cout << myObject[0].getName(); delete [] myObjects; myObjects = NULL;

  7. Pointers -> Arrays • So, there is a connection from pointers to arrays!

  8. Pit stop: static vs. dynamic • What is the difference between a static variable or objects and a dynamic variable or object? • static – allocation and cleanup are automatic • cleanup happens when the variable or object passes out of scope • dynamic – allocation and cleanup are manual • the programmer (you) must do it all • new • delete (remember “delete []” for arrays)

  9. Arrays as class members class MyClass { public: // constructor // CRUD functions private: intmyValues[5]; unsigned int size; }; // Static array, array initialized when object is created // ints in myValuesnot initialized // size must be initialized in constructor

  10. Arrays as class members class MyClass { public: // constructor // CRUD functions private: int* myValues; unsigned int size; }; // Dynamic array, must be initialized in constructor // size must be initialized in constructor

  11. Arrays as class members • Remember • class members can be static or dynamic • CRUD – something to think about • Can you truly delete an element of an array of variables or objects?

  12. Arrays as class members • A challenging scenario • What happens to the static members of a dynamic object? • when the programmer deletes the dynamic object, the static members are cleaned up automatically

  13. Arrays as class members • Another challenging scenario • What happens to the dynamic members of a static object? • the programmer must manually delete the dynamic members

  14. Arrays as class members • And one last challenging scenario • What happens to the dynamic members of a dynamic object? • the programmer must manually delete the dynamic object and its dynamic members

  15. Arrays as class members • How do you manually delete the dynamic members of a class? • the destructor

  16. Arrays -> Destructors • So, there is a connection from arrays to destructors!

  17. Destructors • What are they? • functions that perform cleanup on objects • When are they executed? • when a static object passes out of scope • when a dynamic object is deleted

  18. Destructors • What do they look like? class MyClass { public: ~MyClass (); };

  19. Destructors • What do they look like? MyClass::~MyClass () { }

  20. Destructors • Am I required to implement a destructor for every class that I create? • No – one is provided automatically

  21. Destructors • When must I implement a destructor for my class? • when the class dynamically allocates memory (in most cases)

  22. Destructor • Do I need a destructor for this class? class MyClass { public: // constructor // CRUD functions private: intmyValues[5]; unsigned int size; }; NO

  23. Destructor • Do I need a destructor for this class? class MyClass { public: // constructor // CRUD functions private: int* myValues; unsigned int size; }; MyClass::MyClass () { myValues = new int[5]; size = 5; } YES

  24. Destructor • What would that destructor look like? MyClass::~MyClass () { delete [] myValues; }

  25. Finale

More Related