Review lec5

18
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١ Data Structure Instructor :Nabeel Alassaf Review Pointers Lecture 5

Transcript of Review lec5

Page 1: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١

Data StructureInstructor :Nabeel Alassaf

Review PointersLecture 5

Page 2: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢

Pointer Variables

• Pointer variable: Contain a memory address as their value – Normal variables contain a specific value (direct reference)– Pointers contain the address of a variable that has a specific value

(indirect reference)

• Declaring Pointer Variables: Syntax

• Examples:

int *p;char *ch;

count

7

count7

countPtr

Page 3: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٣

Pointer Variables (continued)

• These statements are equivalentint *p;int* p; int * p;

• The character * can appear anywhere between type name and variable name

• In the statement int* p, q;

only p is the pointer variable, not q; here q is an int variable

int variable

pointervariable

Page 4: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٤

Address of Operator (&)

• The ampersand, &, is called the address of operator

• The address of operator is a unary operator that returns the address of its operand

Page 5: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٥

• & (address operator)− Returns the address of its operand− Example

int y = 5;int *yPtr;yPtr = &y; // yPtr gets address of y

− yPtr “points to” y

yPtry٥

yptr٥٠٠٠٠٠ ٦٠٠٠٠٠

y٦٠٠٠٠٠ ٥

address of y is value of yptr

Page 6: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٦

The following statement prints the value stored in the memory space pointed to by p, which is the value of x.

The following statement stores 55 in the memory location pointed to by p—that is, in x.

X25

P X25

Page 7: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٧

• You can also declare pointers to other data types, such as classes.

•student is an object of type studentType, and studentPtr is a pointer variable of type studentType.

Page 8: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٨

• This statement stores the address of student in studentPtr.

• This statement stores 3.9 in the component gpa of the object student.

Page 9: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٩

The syntax for accessing a class (struct) member using the operator -> is:

Page 10: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٠

Dynamic Variables

• Dynamic variables: created during execution

• C++ creates dynamic variables using pointers

• Two operators, new and delete, to create and destroy dynamic variables

• new and delete are reserved words

Page 11: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١١

• The operator new has two forms: one to allocate a singlevariable, and another to allocate an array of variables. Thesyntax to use the operator new is:

where intExp is any expression evaluating to a positive integer.

• The operator new allocates memory (a variable) of the designated type and returns a pointer to it which is, the address of this allocated memory. and the allocated memory is uninitialized.

Page 12: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٢

• The statement:p = &x;stores the address of x in p. However, no new memory is allocated.

• Consider the following statement:

• This statement creates a variable during program execution somewhere in memory, and stores the address of the allocated memory in p.

• The allocated memory is accessed via pointer dereferencing namely, *p.

Page 13: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٣

• The statement in Line 1 allocates memory space of type intand stores the address of the allocated memory space into p. Suppose that the address of allocated memory space is 1500.

Page 14: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٤

• The statement in Line 2 stores 54 into the memory space that ppoints to.

• The statement in Line 3 executes, which allocates a memory space of type int and stores the address of the allocated memory space into p. Suppose the address of this allocated memory space is 1800.

Store 73

Page 15: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٥

• The statement in Line 4 stores 73 into the memory space that p points.

Store 73

Page 16: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٦

• What happened to the memory space 1500 that p was pointing to after execution of the statement in Line 1?

• After execution of the statement in Line 3, p points to the new memory space at location 1800.

• The previous memory space at location 1500 is now inaccessible.

• The memory space 1500 remains as marked allocated. In other words, it cannot be reallocated.

• This is called memory leak. • How to avoid memory leak.

• When a dynamic variable is no longer needed, it can be destroyed; that is, its memory can be deallocated.

Page 17: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٧

The C++ operator delete is used to destroy dynamic variables. The syntax to use the operator delete has two forms:

EXAMPLE:

Note: the pointers after running the previous statement may still contains the address of a deallocated memory space this pointers called dangling.

Page 18: Review lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٨

Example:( why we use dynamic array)

• The statement in Line 1 declares intList to be a pointer of type int.

• The statement in Line 2 declares arraySize to be an int variable.

• The statement in Line 3 prompts the user to enter the size of the array

• The statement in Line 4 inputs the array size into the variable arraySize.

• The statement in Line 6 creates an array of the size specified by arraySize, and the base address of the array is stored in intList.