Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation. Example: Employee* boss // does not create the object! boss = new Employee or boss = new Employee(parameters); boss is a pointer to the object. *boss is the object. (*boss).method() or bossmethod() is the proper way to access a method. Note the syntax and common errors on p. 309-10. In a method, this is a pointer to the implicit parameter (object specified when the method was called). Key word this is not necessary but some like it. Function new boss = New Employee() allocates memory from the heap for the object itself. Variable boss is allocated on the stack. HEAP ALLOCATED MEMORY MUST BE DISPOSED OF WHEN IT IS NO LONGER USED. Use delete boss at some point. Java has automatic garbage collection C++ does NOT. If not used, the result can be a memory leak (unusable memory). dangling pointers: A pointer that has not been initialized or a pointer after the associated heap memory is deleted. COMMON ERROR is to try to access a dangling pointer. Do example from the snippets file. Address operator: If x is a pointer and y is an object then we can write x=&y. In this case, *x and y are the same thing. x y Do example from the snippets file under Pointers. Try a few things: Set x to y. Reference *x after deleting it. Set x to y and delete y. Set x to y and delete both x and y. Try deleting p. Examine all in the watch window. Display q[0] toward the end of the main. Set up tempdemo for department.cpp. Code on p. 317. Shows why pointers are useful What happens if you write delete tina prior to qc.print() in the main program? Note the comparison between a reference parameter and specifying a pointer as a parameter. p. 319. Important! Discuss common errors on p. 329. Demo04: listing of accounts for a customer is done by going through ALL account objects. This is NOT a good design. Demo06 uses an array of pointers inside the customer class to locate a list of account objects. Arrays and pointers: int a[10] similar to int* a and a = new int[10] Could also use a = new int[n], where n is input by the user. a[i] same as *(a+i) This is a dynamic array in contrast to a static array discussed previously. Example int a=99; int * x; int b=99; x = new int[5]; for (int i=0; i<5; i++) *(x+i)=i*i; for (int i=0; i<5; i++) cout << x[i] << " "; cout << endl; cout << "a is " << a << " b is " << b << endl; cout << endl; See advance topic on p. 323 See Quality Tip on p. 324 COMP SCI 370 may cover this in more detail. See common error 7.6 on p. 325 and demo the code as written in the notes. See advanced topic on p. 326. Do, also, 2D dynamic arrays. In particular look at code from the snippet file (optional) While running this program take a look at the debugger. In particular put m, m[0], m[1], etc in a watch window. Also, while running, take a look at the memory window to see the actual contents of memory. Pointers to character strings: Explain the difference between char c = ‘A’ and char* c = “A”. This is a COMMON problem in C. Explain problem with comparing char* p and char * q with p == q using this example #include <iostream> using namespace std; int main( ) { char* x = "Harry"; char * y; y=new char[6]; strcpy(y, "Harry"); cout << "x is " << x << " y is " << y << endl; if (x == y) cout << "strings are equal"; else cout << "string are NOT equal"; return 0; }