POINTERS - PowerPoint PPT Presentation

About This Presentation
Title:

POINTERS

Description:

Are pointers restricted in the types of objects they can point to? ... dereferencing - pointer arithmetic - parameter passing - pointers to functions ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 12
Provided by: eckerdwor
Category:

less

Transcript and Presenter's Notes

Title: POINTERS


1
POINTERS
2
Pointers
A pointer type is one in which the variable has
a range of values corresponding to memory
addresses and a special value, nil, which means
an invalid address.
3
Pointers design issues
  • What is the scope and lifetime of a pointer
    variable?
  • What is the lifetime of a dynamic variable?
  • Are pointers restricted in the types of objects
    they can point to?
  • Are pointers used for dynamic storage
    management, indirect addressing or both?

4
Pointers problems
  • Type checking -
  • the type a pointer can point to is called its
    domain type
  • Dangling pointers -
  • the pointer contains the address of a
    variable
  • which has been deallocated
  • Lost objects -
  • the pointer is destroyed, but the data to
  • which it points, may still be needed

5
Pointers problems
  • in Pascal
  • dangling pointers can be created
    with specific deallocation

6
Pointers problems
In Pascal, dangling pointers can be created with
specific deallocation
Pascal implementors can - ignore the dispose -
not include dispose in the language - deallocate
the dynamic variable and set its pointer to
nil - implement dispose completely and correctly
disallowing dangling pointers
7
Pointers problems
In C, dangling pointers and lost objects can be
created as below
Dangling pointer int p1, p2 p1 (int )
malloc (sizeof(int)) p2 p1 free (p1)
p2 is now a dangling pointer
8
Pointers problems
In C, dangling pointers and lost objects can be
created as below
Dangling pointer int p1, p2 p1 new
int p2 p1 delete p1 p2 is now a
dangling pointer
9
Pointers problems
In C, dangling pointers and lost objects can be
created
Lost object int p1 p1 (int ) malloc
(5sizeof(int)) ... p1 (int ) malloc
(10sizeof(int)) The first block of allocated
memory is now a lost object.
10
Pointers problems
In C, dangling pointers and lost objects can be
created
Lost object int p1 p1 new int10
... p1 new int15 The first block of
allocated memory is now a lost object.
11
Pointers problems
In C/C, dangling pointers and lost objects can
be created, but additional capabilities are
available
- dereferencing - pointer arithmetic -
parameter passing - pointers to functions
Write a Comment
User Comments (0)
About PowerShow.com