CMPUT 201: Lab 5 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CMPUT 201: Lab 5

Description:

... it will eat up all remaining memory in the heap and the system will shut down ... This code correctly stores the address of the next node in a temporary variable ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 13
Provided by: nla5
Category:
Tags: cmput | down | lab | of | system

less

Transcript and Presenter's Notes

Title: CMPUT 201: Lab 5


1
CMPUT 201 Lab 5
Pointers Memory Leaks
October 17th, 2002
2
Uninitialized Memory
  • Pointer variables have meaningless values in
    them when they are declared
  • Like any other data type, you must initialize a
    pointer with a proper value before you can use it
  • You can initialize a pointer with either
    another pointers value or by allocating dynamic
    memory with new
  • If you fail to do either of these two things
    and then try to access that pointers address,
    your program will misbehave and probably crash

3
Freed Memory
  • Uninitialized memory accesses can occur before
    a pointer lifespan begins freed memory accesses
    can occur after a pointers lifespan ends
  • When you return the memory associated with a
    pointer using delete, the address remains in
    the pointer variable but is no longer valid
  • Since the memory at that address has been
    freed, accessing that pointers address causes
    the same disastrous behaviour as using
    uninitialized memory

4
Bad Memory Use Examples
  • Consider the code fragments below
  • // Example 1ListNode nodePtr
  • // wrong - uses uninitialized memory!
  • If (nodePtr-value 0)
  • . . .// Example 2delete(nodePtr)
  • // wrong - uses freed memory!If (nodePtr-value
    10)
  • . . .

5
Memory Leaks
  • Memory leaks occur when you lose the ability to
    access dynamic memory
  • They do not cause the program to crash right
    away, but limit memory throughout its execution
  • If a memory leak is severe enough, it will eat
    up all remaining memory in the heap and the
    system will shut down
  • At the very least, there will be a progressive
    degradation of your programs performance

6
Memory Leak Example
  • Consider the code fragment below
  • // pointer to head of listlistHeadPtr new
    ListNode// pointer to current nodetempNodePtr
    listHeadPtr
  • for (int i0 i tempNodePtr.next new ListNode tempNodePtr
    tempNodePtr.next tempNodePtr.value
    someValuessomeHashFn(i)
  • . . .
  • delete ListHeadPtr

7
Memory Leak Example
  • In memory the list created in the previous code
    sample looks like

ListHeadPtr 0x0380
Next 0x03A0
Next0x03C0
Next(null)
8
Memory Leak Example
  • The program statement delete(ListHeadPtr)
    alters memory in the following way

ListHeadPtr 0x0380
What happens to these nodes?
Next 0x03A0
Next0x03C0
Next(null)
9
Memory Leak Analysis
  • Only the first node is properly deleted all
    others are lost
  • Since we dont have any pointer variables
    containing the address of the second list node,
    we cant return the memory associated with that
    node or the rest of the list
  • We need to iterate through the list and return
    the memory associated with each node

10
Correcting the Memory Leak
  • The following code properly returns all memory
    associated with the list
  • currentNodePtr listHeadPtr ListNode
    nextNodePtr
  • while (currentNodePtr ! NULL) nextNodePtr
    currentNodePtr-next delete(currentNodePtr)
    currentNodePtr nextNodePtr
  • This code correctly stores the address of the
    next node in a temporary variable before deleting
    the current node. This way, we still have a way
    of accessing the next node in the list.

11
Question 5 ??
  • Heres how the tree is stored in memory

firstChild0x0D00
nextSibling(null)
firstChild(null)
firstChild(null)
firstChild(null)
nextSibling0x0D10
nextSibling0x0D20
nextSibling(null)
12
Question 5 ??
  • Consider this analysis of the original code
  • // assign to nextChildPtr the address of
  • // the firstChild field in the root node
  • Tree nextChildPtr root-_firstChild
  • for (Tree child makeTree(istrm) child
    child makeTree(istrm))
  • // assign to the field to which nextChildPtr
  • // points the address of the latest node
  • nextChildPtr child
  • // store the address of the nextSibling
  • // field in the child node
  • nextChildPtr child-_nextSibling
Write a Comment
User Comments (0)
About PowerShow.com