Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.

Similar presentations


Presentation on theme: "Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation."— Presentation transcript:

1 Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.

2 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 boss  method() is the proper way to access a method.

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

4 Function new boss = New Employee() allocates memory from the heap for the object itself. Variable boss is allocated on the stack.

5 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).

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

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

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

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

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

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

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

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

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

15 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

16 #include 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; }


Download ppt "Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation."

Similar presentations


Ads by Google