Pointers and Dynamic Memory Management - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Pointers and Dynamic Memory Management

Description:

When a variable is declared, enough memory to hold a value of that type is ... top = -1; items = new ItemType[max]; // dynamically allocates array. 44 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 50
Provided by: anne277
Category:

less

Transcript and Presenter's Notes

Title: Pointers and Dynamic Memory Management


1
Pointers and Dynamic Memory Management
  • CSC 2110 - Data Structures Abstraction
  • Spring/Summer 2001
  • Wayne State University
  • Instructor Anne-Marie Bosneag

2
Content
  • Pointer types
  • Dereferencing
  • The NULL pointer
  • Pointer arithmetic
  • Dynamically allocated memory
  • Arrays as pointers
  • Stacks queues implemented by dynamic arrays

3
C Data Types
Simple
Structured
Integral
Floating
array struct union class
char short int long enum
float double long double
4
Addresses in Memory
  • When a variable is declared, enough memory to
    hold a value of that type is allocated for it at
    an unused memory location. This is the address
    of the variable.
  • int x
  • float number
  • char ch
  • 2000 2002
    2006

5
Pointer Types
  • In C, variables have a
  • name
  • type
  • value
  • location in memory (or address)

6
Example
  • int alpha 20, beta 30
  • Address Memory cell Program identifier
  • 2000 alpha
  • 2001 beta

20
30
7
What is a pointer variable?
  • A pointer variable is a variable whose value is
    the address of a location in memory.
  • To declare a pointer variable, you must specify
    the type of value that the pointer will point
    to.
  • For example,
  • int ptr // ptr will hold the address of an
    int
  • char q // q will hold the address of a
    char

8
Pointer Types
  • ltTypegt ltidentifiergt
  • Ex int p // p is a pointer to an integer
  • Note 1. The placement of is not important
  • int p
  • int p
  • int p
  • 2. int p, q is equivalent to int p int q

9
Using a pointer variable
2000
12 x 3000 2000 ptr
  • int x
  • x 12
  • int ptr
  • ptr x
  • NOTE Because ptr holds the address of x,
  • we say that ptr points to x

10
Obtaining Memory Addresses
  • The address of a non-array variable can be
    obtained by using the address-of operator .
  • int x
  • float number
  • char ch
  • cout ltlt Address of x is ltlt x ltlt endl
  • cout ltlt Address of number is ltlt number ltlt
    endl
  • cout ltlt Address of ch is ltlt ch ltlt endl

11
Unary operator
2000
12 x 3000 2000 ptr
  • int x
  • x 12
  • int ptr
  • ptr x
  • cout ltlt ptr
  • NOTE The value pointed to by ptr is denoted
    by ptr

12
Using the dereference operator
2000 12
5 x 3000 2000 ptr
  • int x
  • x 12
  • int ptr
  • ptr x
  • ptr 5 // changes the value // at
    adddress ptr to 5

13
Another Example
4000 A Z
ch 5000
6000 4000 4000 q
p
  • char ch
  • ch A
  • char q
  • q ch
  • q Z
  • char p
  • p q // the right side has value 4000
  • // now p and q both point to ch

14
The Null Pointer
  • During program execution, a pointer
  • variable can be in one of the following
  • states
  • 1. it is unassigned
  • 2. it points to some data object
  • 3. It contains the pointer constant 0
  • (or null pointer)

15
The NULL Pointer
  • There is a pointer constant 0 called the null
    pointer denoted by NULL in stddef.
  • But NULL is not memory address 0!
  • NOTE It is an error to dereference a pointer
    whose value is NULL. Such an error may cause
    your program to crash, or behave erratically.
    It is the programmers job to check for this.
  • while (ptr ! NULL)
  • . . . // ok to use ptr here

16
The NULL pointer
  • Pointer value can be tested for
  • if (p 0)
  • ...
  • Instead of constant 0, it is better to
  • use a constant identifer -- NULL
  • include ltstddef.hgt
  • ...
  • p NULL
  • ...

17
Allocation of memory
STATIC ALLOCATION Static
allocation is the allocation of memory
space at compile time.
DYNAMIC ALLOCATION Dynamic
allocation is the allocation of memory space at
run time by using operator new.
18
3 Kinds of Program Data
  • STATIC DATA memory allocation exists throughout
    execution of program.
  • static long SeedValue
  • AUTOMATIC DATA automatically created at function
    entry, resides in activation frame of the
    function, and is destroyed when returning from
    function.
  • DYNAMIC DATA explicitly allocated and
    deallocated during program execution by C
    instructions written by programmer using unary
    operators new and delete

19
Using operator new
  • If memory is available in an area called the
    free store (or heap), operator new allocates the
    requested object or array, and returns a pointer
    to (address of ) the memory allocated.
  • Otherwise, the null pointer 0 is returned.
  • The dynamically allocated object exists until
    the delete operator destroys it.

19
20
Dynamically Allocated Data
  • char ptr
  • ptr new char
  • ptr B
  • cout ltlt ptr

ptr
2000
21
Dynamically Allocated Data
  • char ptr
  • ptr new char
  • ptr B
  • cout ltlt ptr
  • NOTE Dynamic data has no variable name

2000 ptr
22
Dynamically Allocated Data
  • char ptr
  • ptr new char
  • ptr B
  • cout ltlt ptr
  • NOTE Dynamic data has no variable name

23
Dynamically Allocated Data
2000 ptr NOTE Delete
deallocates the memory pointed to by
ptr.
  • char ptr
  • ptr new char
  • ptr B
  • cout ltlt ptr
  • delete ptr

?
24
Using operator delete
  • The object or array currently pointed to by the
    pointer is deallocated, and the pointer is
    considered unassigned. The memory is returned to
    the free store.
  • Square brackets are used with delete to
    deallocate a dynamically allocated array of
    objects.

24
25
Some C pointer operations
  • Precedence
  • Higher -gt Select
    member of class pointed to
  • Unary -- !
    new delete
  • Increment,
    Decrement, NOT, Dereference, Allocate,
    Deallocate
  • - Add Subtract
  • lt lt gt gt Relational
    operators
  • ! Tests for equality,
    inequality
  • Lower Assignment

26
Pointer Arithmetic
  • int vec 3 , vptr
  • vec 0 5
  • vec 1 10
  • vec 2 15
  • vptr vec
  • vptr 5
  • (vptr 1) 10
  • (vptr 2) 15

27
Pointer Arithmetic
  • The following formula can be used to find
  • the pointerss address
  • p p si
  • where
  • p is the new pointer value
  • p is the current pointer value
  • s is the size (in bytes) of type that is pointed
    to
  • i is the value added to the pointer

28
Example
  • int i 10, ip i
  • if i is located at address 210 in memory,
  • then ip
  • makes ip to point to location 212

29
Example
  • int a 100 , p a
  • p 10
  • / Increment address p and then dereference it.
  • That is, dereference a 1 /

30
Example
  • struct block
  • float height, width
  • int color
  • block blk, p blk
  • (p).color 5 / Note that . has higher
  • precedence than /
  • We can write the above statement as follows
  • p -gt color 5

31
Recall that . . .
  • char msg 8
  • msg is the base address of the array. We say
    msg is a pointer because its value is an address.
    It is a pointer constant because the value of
    msg itself cannot be changed by assignment. It
    points to the memory location of a char.

32
Pointers as Arrays Arrays as Pointers
a
  • int a 3 , p
  • p a
  • / The same as
  • p a 0 /
  • a 0 2

address
a 0 2 2000
a 1 2002
a 2 2004
p
33
  • p 10 is equivalent to a 0 10
  • a 20 is equivalent to a 0 20
  • Note
  • a p is not allowed!
  • Array name is a constant pointer

34
Dynamic Array Allocation
char ptr // ptr is a pointer variable
that // can hold the
address of a char ptr new char 5
// dynamically, during run time, allocates
// memory for 5 characters and places into
// the contents of ptr their beginning
address
6000
6000
ptr
35
Dynamic Array Allocation
char ptr ptr new char 5 strcpy
(ptr, Bye) ptr 1 u // a pointer
can be subscripted cout ltlt ptr 2
6000
u
6000
B y e \0
ptr
36
Dynamic Array Deallocation
char ptr ptr new char 5 ptr
1 u delete ptr // deallocates
array pointed to by ptr //
ptr itself is not deallocated, but
// the value of ptr is considered
unassigned
?
ptr
37
What happens here?
  • int ptr new int
  • ptr 3
  • ptr new int // changes value of ptr
  • ptr 4

3 ptr
3 ptr 4
38
Memory Leak
  • A memory leak occurs when dynamic memory (that
    was created using operator new) has been left
    without a pointer to it by the programmer, and so
    is inaccessible.
  • int ptr new int
  • ptr 8
  • int ptr2 new int
  • ptr2 -5
  • How else can an object become inaccessible?

8 ptr
-5 ptr2
39
Causing a Memory Leak
8 ptr
-5 ptr2
  • int ptr new int
  • ptr 8
  • int ptr2 new int
  • ptr2 -5
  • ptr ptr2 // here the 8 becomes
    inaccessible

8 ptr
-5 ptr2
40
A Dangling Pointer
  • occurs when two pointers point to the same
    object and delete is applied to one of them.
  • int ptr new int
  • ptr 8
  • int ptr2 new int
  • ptr2 -5
  • ptr ptr2

41
Leaving a Dangling Pointer
  • int ptr new int
  • ptr 8
  • int ptr2 new int
  • ptr2 -5
  • ptr ptr2
  • delete ptr2 // ptr is left dangling
  • ptr2 NULL

8 ptr NULL
ptr2
42
DYNAMIC ARRAY IMPLEMENTATION
class StackType
StackType
Private Data top 2 maxStack
5 items
50 43 80
StackType
items 0 items 1 items 2 items 3 items
4
Push
Pop . . .
43
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS
  • include "ItemType.h" // for ItemType
  • templateltclass ItemTypegt
  • class StackType
  • public
  • StackType( )
  • StackType( int max ) // PARAMETERIZED
    CONSTRUCTOR
  • StackType( ) // DESTRUCTOR
  • . . .
  • bool IsFull( ) const
  • void Push( ItemType item )
  • void Pop( ItemType item )
  • private
  • int top
  • int maxStack
  • ItemType items // DYNAMIC ARRAY
    IMPLEMENTATION

43
44
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS
    contd
  • //------------------------------------------------
    --------
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType( ) //DEFAULT
    CONSTRUCTOR
  • maxStack 500
  • top -1
  • items new ItemType500 // dynamically
    allocates array
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType( int max ) //
    PARAMETERIZED
  • maxStack max
  • top -1
  • items new ItemTypemax // dynamically
    allocates array

44
45
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS
    contd
  • //------------------------------------------------
    --------
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType( )
  • delete items // deallocates array
  • .
  • .
  • .
  • templateltclass ItemTypegt
  • bool StackTypeltItemTypegtIsFull( )
  • return (top maxStack - 1)

45
46
DYNAMIC ARRAY IMPLEMENTATION
class QueType
QueType
Private Data front 1 rear
4 maxQue 5 items
QueType
Enqueue
C X J
RESERVED
Dequeue . . .
items 0 1 2 3 4
47
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE
  • include "ItemType.h" // for ItemType
  • templateltclass ItemTypegt
  • class QueType
  • public
  • QueType( )
  • QueType( int max ) // PARAMETERIZED
    CONSTRUCTOR
  • QueType( ) // DESTRUCTOR
  • . . .
  • bool IsFull( ) const
  • void Enqueue( ItemType item )
  • void Dequeue( ItemType item )
  • private
  • int front
  • int rear
  • int maxQue
  • ItemType items // DYNAMIC ARRAY
    IMPLEMENTATION

47
48
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE
    contd
  • //------------------------------------------------
    --------
  • templateltclass ItemTypegt
  • QueTypeltItemTypegtQueType( int max ) //
    PARAMETERIZED
  • maxQue max 1
  • front maxQue - 1
  • rear maxQue - 1
  • items new ItemTypemaxQue // dynamically
    allocates
  • templateltclass ItemTypegt
  • bool QueTypeltItemTypegtIsEmpty( )
  • return ( rear front )

48
49
  • //------------------------------------------------
    --------
  • // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE
    contd
  • //------------------------------------------------
    --------
  • templateltclass ItemTypegt
  • QueTypeltItemTypegtQueType( )
  • delete items // deallocates array
  • .
  • .
  • .
  • templateltclass ItemTypegt
  • bool QueTypeltItemTypegtIsFull( )
  • // WRAP AROUND
  • return ( (rear 1) maxQue front )

49
Write a Comment
User Comments (0)
About PowerShow.com