10-cpp-review

10-cpp-review - 1 CS216: Program and Data Representation...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield C++ Review Review of pointers, dereferencing, new and delete C++ new, delete, pointers, &, * !!! 3 Review * pointer, when in a declaration statement Rectangle *someRectangle; dereferences all other times Accesses the memory address held in the pointer variable & reference in a declaration List & someList = new List; void printList(const List & theList); address of Rectangle anotherRectangle; someRectangle = &anotherRectangle; 4 Pointer Variables are Typed double Z = 26.0; int * someInt = &Z; cannot convert 'double*' to 'int*' in initialization pointer to int Address of double 5 Dereferencing: Whats printed? int n = 26; int * somePointer = &n; cout << somePointer << endl; cout << *somePointer << endl; 6 Dereferencing: Whats printed? int n = 26; int * somePointer = &n; *somePointer = 45; cout << *somePointer << endl; cout << n << endl; 7 Whats the Difference Between These? ListNode * ptr1, * ptr2; ptr1 = new ListNode; ptr2 = new ListNode; bool result1 = (ptr1 == ptr2); bool result2 = (*ptr1 == *ptr2); 8 References int y = 5; int &x = y; We can print the value 5 easily: cout << x << endl; C++ automatically dereferences the reference How do we print the address in x? cout << &x << endl; Thats getting the address for a automatic dereference! How do we change the address of x? 9 End of lecture on Fri, Mar 27 Also did slides 79-92 of 09-x86 10 Pointer and Memory Allocation Pitfalls Uninitialized pointers Dereferencing NULL pointers Dangling pointer Losing address of dynamically allocated memory 11 Pointers and Types int main() { Animal cow(); Animal* cowPtr1 = &cow; Animal** cowPtr2(&cowPtr1); Animal*** cowPtr3 = &cowPtr2; } What types are cow, cowPtr1, cowPtr2, cowPtr3? 12 Summary Variable Name Type of Variable Mem.Addr (made up) Value Stored cow Animal 108 500 cowPtr1 Animal * 110 108 cowPtr2 Animal ** 112 110 cowPtr3 Animal *** 114 112 13 How Can We Change cows Weight? Assume there is a setWeight(int weight) method cow cowPtr1 cowPtr2 cowPtr3 14 The Tree code in Weiss void insert (const Comparable &x, BinaryNode * & t) BinaryNode has a left and a right pointer What would happen if the second parameter was only a pointer As opposed to a reference to a pointer? Pointers, new, delete, and Memory Leaks Is this code correct?...
View Full Document

This note was uploaded on 09/11/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Page1 / 46

10-cpp-review - 1 CS216: Program and Data Representation...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online