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

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

Embed Size (px)

Citation preview

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

Chapter 7 Pointers:

Java does not have pointers.Used for dynamic memory allocation.

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

Example:

Employee* boss // does not create the object!boss = new Employee orboss = 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.

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

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.

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

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

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

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

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

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.

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

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

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

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.

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

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!

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

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.

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

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.

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

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

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

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.

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

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.

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

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

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

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