Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

38
Chapter 6 Data Types

Transcript of Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

Page 1: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

Chapter 6

Data Types

Page 2: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

2

Page 3: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

3

Page 4: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

4

Page 5: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

5

Page 6: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

6

Page 7: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

7

Page 8: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

8

Page 9: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

9

Page 10: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

10

Page 11: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

11

Page 12: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

12

Page 13: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

13

Page 14: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

14

Page 15: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

15

Page 16: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

16

Page 17: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

17

Page 18: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

18

Page 19: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

19

Page 20: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

20

Page 21: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

21

Page 22: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

22

Page 23: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

23

Page 24: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

24

Page 25: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

25

Page 26: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

26

Page 27: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

27

Page 28: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

28

Page 29: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

29

Pointer Type

A pointer has a memory address as it value (r-value), and also has a special value, nil (NULL in C)

Pointer operations:

•Assignment: Sets a pointer variable’s value to some useful address(&) .

•Dereferncing: Fetches the value in the memory cell whose address is referenced

by the pointer(*) .

Page 30: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

30

Pointer Types (C)

#include<stdio.h;>int main{()

int k, n; int *p;//Pointer declaration

k = 560; p = &k;//Pointer assignment

n = *p;//Pointer dereferencing printf("\n n = %d ",n); //Prints 560

return(0);

}

Page 31: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

31

Pointer Variables - Applications

Indirect addressing ( p=&k )

Array addressing ( *(mat+3) )

Record Field addressing ( s->id )

Allocation of dynamic storage

)p = (int*) malloc(64) ;(

Page 32: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

32

Dangling Reference

•A dangling reference is a pointer that contains address of a dynamic variable that has been

deallocated .

–Pointer “p1” is set to point to a new heap-dynamic variable.

–Pointer “p2” is assigned p1’s value.–The heap dynamic variable pointed to by “p1” is

explicitly deallocated , and p1 is set to null. Pointer p2 is not changed. p2 is now a dangling reference.

Page 33: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

33

Dangling Reference

Heap

p1

p2

Heap

p1 (NULL)

p2

(Deallocated)

Page 34: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

34

Dangling Reference

ProblemsThe location being pointed to by p2 may have been reallocated to some new heap-dynamic

variable .

The new value will bear no relationship with the old pointer’s dereferenced value .

If p2 is used to change the heap dynamic variable, then the value of the new heap dynamic variable will be destroyed.

Page 35: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

35

Lost ObjectA lost heap-dynamic variable (lost object) is an allocated heap-dynamic variable that is no longer accessible to the user program, but still contains some useful data.

Pointer p1 is set to point to a heap-dynamic variable.

p1 is set to point to another heap dynamic variable. The first heap-dynamic variable is now inaccessible, or lost.

Page 36: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

36

Lost Object

Heap

p1

Heap

p1

Heap

Lost object

Page 37: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

37

Pointers and Java

•Java does not provide indirect addressing (&) and dereferencing

operators .(*)

•Java also does not allow any pointer arithmetic .

•Java does not allow explicit deallocation of storage. There is no explicit deallocation operator (such as “delete”).

Since storage cannot be explicitly deallocated, you cannot have a dangling reference in Java.

Page 38: Chapter 6 Data Types. 2 3 4 5 6 7 8 9 10.

38

Heap Management

•Never reassign a pointer variable which has a dynamic storage allocation. Before reassigning a pointer, make sure that the storage it points to, is deallocated.

•Immediately after deallocating a heap-dynamic storage, reset to null all the pointer variables pointing to it.

•Whenever a large storage allocation from heap is requested, check if enough memory is available.