0% found this document useful (0 votes)
8 views

Pointers

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Pointers

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Pointer and Reference Types

• A pointer type variable has a range of


values that consists of memory addresses
and a special value, nil
• Provide the power of indirect addressing
• Provide a way to manage dynamic memory
• A pointer can be used to access a location
in the area where storage is dynamically
created (usually called a heap)

Copyright © 2015 Pearson. All rights reserved. 1-1


Design Issues of Pointers

• What are the scope of and lifetime of a


pointer variable?
• What is the lifetime of a heap-dynamic
variable?
• Are pointers restricted as to the type of
value to which they can point?
• Are pointers used for dynamic storage
management, indirect addressing, or both?
• Should the language support pointer types,
reference types, or both?

Copyright © 2015 Pearson. All rights reserved. 1-2


Pointer Operations

• Two fundamental operations: assignment


and dereferencing
• Assignment is used to set a pointer
variable’s value to some useful address
• Dereferencing yields the value stored at the
location represented by the pointer’s value
– Dereferencing can be explicit or implicit
– C++ uses an explicit operation via *
j = *ptr
sets j to the value located at ptr

Copyright © 2015 Pearson. All rights reserved. 1-3


Pointer Assignment Illustrated

The assignment operation j = *ptr

Copyright © 2015 Pearson. All rights reserved. 1-4


Pointers in C and C++

• Extremely flexible but must be used with care


• Pointers can point at any variable regardless of
when or where it was allocated
• Used for dynamic storage management and
addressing
• Pointer arithmetic is possible
• Explicit dereferencing and address-of operators
• Domain type need not be fixed (void *)
void * can point to any type and can be type
checked (cannot be de-referenced)

Copyright © 2015 Pearson. All rights reserved. 1-5


Pointer Arithmetic in C and C++

float stuff[100];
float *p;
p = stuff;

*(p+5) is equivalent to stuff[5] and p[5]


*(p+i) is equivalent to stuff[i] and p[i]

Copyright © 2015 Pearson. All rights reserved. 1-6


Problems with Pointers

• Dangling pointers (dangerous)


– A pointer points to a heap-dynamic variable that has been
deallocated
• Lost heap-dynamic variable
– An allocated heap-dynamic variable that is no longer
accessible to the user program (often called garbage)
• Pointer p1 is set to point to a newly created heap-dynamic
variable
• Pointer p1 is later set to point to another newly created
heap-dynamic variable
• The process of losing heap-dynamic variables is called
memory leakage

Copyright © 2015 Pearson. All rights reserved. 1-7


Reference Types

• C++ includes a special kind of pointer type


called a reference type that is used
primarily for formal parameters
– Advantages of both pass-by-reference and
pass-by-value
• Java extends C++’s reference variables and
allows them to replace pointers entirely
– References are references to objects, rather than
being addresses
• C# includes both the references of Java and
the pointers of C++

Copyright © 2015 Pearson. All rights reserved. 1-8


Evaluation of Pointers

• Dangling pointers and dangling objects are


problems as is heap management
• Pointers are like goto's--they widen the
range of cells that can be accessed by a
variable
• Pointers or references are necessary for
dynamic data structures--so we can't design
a language without them

Copyright © 2015 Pearson. All rights reserved. 1-9


Dangling Pointer Problem

• Tombstone: extra heap cell that is a pointer to the


heap-dynamic variable
– The actual pointer variable points only at tombstones
– When heap-dynamic variable de-allocated, tombstone
remains but set to nil
– Costly in time and space
. Locks-and-keys: Pointer values are represented as
(key, address) pairs
– Heap-dynamic variables are represented as variable plus
cell for integer lock value
– When heap-dynamic variable allocated, lock value is
created and placed in lock cell and key cell of pointer

Copyright © 2015 Pearson. All rights reserved. 1-11


Heap Management

• A very complex run-time process


• Single-size cells vs. variable-size cells
• Two approaches to reclaim garbage
– Reference counters (eager approach):
reclamation is gradual
– Mark-sweep (lazy approach): reclamation occurs
when the list of variable space becomes empty

Copyright © 2015 Pearson. All rights reserved. 1-12


Reference Counter

• Reference counters: maintain a counter in


every cell that store the number of pointers
currently pointing at the cell
– Disadvantages: space required, execution time
required, complications for cells connected
circularly (Friedman and Wise 1979)
– Advantage: it is intrinsically incremental, so
significant delays in the application execution
are avoided

Copyright © 2015 Pearson. All rights reserved. 1-13


Mark-Sweep

• The run-time system allocates storage cells as


requested and disconnects pointers from cells
as necessary; mark-sweep then begins
– Every heap cell has an extra bit used by collection
algorithm
– All cells initially set to garbage
– All pointers traced into heap, and reachable cells
marked as not garbage
– All garbage cells returned to list of available cells
– Disadvantages: in its original form, it was done too
infrequently. When done, it caused significant delays in
application execution. Contemporary mark-sweep
algorithms avoid this by doing it more often—called
incremental mark-sweep
Copyright © 2015 Pearson. All rights reserved. 1-14
Marking Algorithm

Copyright © 2015 Pearson. All rights reserved. 1-15


Variable-Size Cells

• All the difficulties of single-size cells plus


more
• Required by most programming languages
• If mark-sweep is used, additional problems
occur
– The initial setting of the indicators of all cells in
the heap is difficult
– The marking process in nontrivial
– Maintaining the list of available space is another
source of overhead

Copyright © 2015 Pearson. All rights reserved. 1-16

You might also like