chapter 6 data types

38
Chapter 6 Data Types

Upload: loan

Post on 25-Feb-2016

29 views

Category:

Documents


2 download

DESCRIPTION

Chapter 6 Data Types . 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. (&) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6 Data Types

Chapter 6

Data Types

Page 2: Chapter 6 Data Types

2

Page 3: Chapter 6 Data Types

3

Page 4: Chapter 6 Data Types

4

Page 5: Chapter 6 Data Types

5

Page 6: Chapter 6 Data Types

6

Page 7: Chapter 6 Data Types

7

Page 8: Chapter 6 Data Types

8

Page 9: Chapter 6 Data Types

9

Page 10: Chapter 6 Data Types

10

Page 11: Chapter 6 Data Types

11

Page 12: Chapter 6 Data Types

12

Page 13: Chapter 6 Data Types

13

Page 14: Chapter 6 Data Types

14

Page 15: Chapter 6 Data Types

15

Page 16: Chapter 6 Data Types

16

Page 17: Chapter 6 Data Types

17

Page 18: Chapter 6 Data Types

18

Page 19: Chapter 6 Data Types

19

Page 20: Chapter 6 Data Types

20

Page 21: Chapter 6 Data Types

21

Page 22: Chapter 6 Data Types

22

Page 23: Chapter 6 Data Types

23

Page 24: Chapter 6 Data Types

24

Page 25: Chapter 6 Data Types

25

Page 26: Chapter 6 Data Types

26

Page 27: Chapter 6 Data Types

27

Page 28: Chapter 6 Data Types

28

Page 29: Chapter 6 Data Types

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

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

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

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

33

Dangling Reference

Heapp1

p2

Heapp1 (NULL)

p2

(Deallocated)

Page 34: Chapter 6 Data Types

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

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

36

Lost Object

Heapp1

Heapp1

Heap

Lost object

Page 37: Chapter 6 Data Types

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

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.