Dynamic memory allocation - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Dynamic memory allocation

Description:

Dangling pointers. An illegal pointer dereference results if ... object that no longer exists (dangling pointer) ... dereferencing a dangling pointer ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 17
Provided by: ugrad2
Category:

less

Transcript and Presenter's Notes

Title: Dynamic memory allocation


1
  • Dynamic memory allocation
  • Our first IntVector class has some serious
    limitations
  • the capacity is fixed at MAX_SIZE elements
  • to change capacity, we change MAX_SIZE in the
    header
  • then we recompile the program not pretty!
  • We get around this by using dynamic memory
    allocation
  • memory is given to an object at run-time
  • each object gets what it needs as it needs it
  • dynamic memory requires the use of C pointers

2
Pointers A pointer is a data type whose value is
the address of an object in memory char
charPtrchar myChar A We point to an
object using the or address of
operator charPtr myChar We can change the
value of myChar indirectly using the or
dereferencing operator charPtr B
???
3
  • Constant pointers to objects
  • char char1 A char char2 Bchar const
    charPtr char1
  • the variable charPtr is a constant pointer to
    char
  • the address in charPtr must be defined by
    initialization
  • the address in charPtr cannot be changed
    afterward
  • charPtr char2
  • you can change the object pointed to
  • charPtr C

4
  • Pointers to constant objects
  • char char1 A const char char2 Bconst
    char charPtr char1
  • the variable charPtr is a pointer to a constant
    char
  • the address in charPtr can be changed any time
  • the character pointed to cannot be changed
  • charPtr C char2 D
  • you can change the object pointed to and char1
    itself
  • charPtr char2 char1 E

5
  • Reference types
  • char myChar1 A
  • const char myChar2 Bchar charRef1
    myChar1
  • const char charRef2 myChar2
  • like a constant pointer, but better!
  • charRef1 is a reference to the character myChar1
  • it stores the address of the variable that it is
    referencing
  • this address cannot be changed once it has been
    assigned
  • automatic dereference (no operator)
  • charRef1 charRef2
  • but not for a constant reference
  • charRef2 C

6
Reference parameters Reference variables are
often used as function parameters formal
parameters become references to actual
parameters (more about parameter-passing in a
future lecture) void swap( int num1, int num2
) int temp temp num1 num1 num2
num2 temp int x 2, y 5swap( x, y )
What does this look like in memory? (well talk
more about this in future)
7
  • Dynamic memory allocation (after learning
    pointers)
  • C has three kinds of variables
  • automatic variables allocated and de-allocated
    as needed
  • static variables allocated for the lifetime of
    the execution
  • dynamic variables allocated and de-allocated
    explicitly during execution as specified by the
    program
  • Dynamic memory allocation allows
  • memory efficiency (use only what you need)
  • array (and object) sizes that vary at run time
  • more sophisticated data structures and algorithms

8
The new operator new returns the address of a
newly allocated object int intPtr intPtr new
int We can also allocate an array of
integers int capacity cout ltlt How many
integers do you want to store?\ncin gtgt
capacityint data new int capacity for(
int index 0 index lt capacity index ) cin
gtgt data index
???
n says how much to allocate
9
  • Garbage collection
  • Dynamic memory must be released when no longer
    required
  • some languages have automatic garbage collection
    (Java)
  • C requires explicit garbage collection by the
    program
  • memory is released using the delete operator
  • delete intPtr
  • delete data
  • delete operator does not delete the pointer
  • it releases the memory to which the pointer
    points
  • delete operator will not change the pointer
  • it still points to the memory that was just
    released!
  • this is a problem

10
  • Dangling pointers
  • An illegal pointer dereference results if the
    pointer is
  • not initialized (it could point anywhere)
  • NULL (its value is zero so it points nowhere)
  • referencing an object that no longer exists
    (dangling pointer)
  • referencing outside the object it points to
  • Bad pointer arithmetic usually causes the last
    case
  • int p new int3
  • (p 4) p5
  • both attempts dereference outside the dynamic
    array
  • so they are illegal pointer dereferences

11
  • Avoiding dangling pointers
  • After using the delete operator
  • it is often a good idea to set the pointer to 0
  • the built-in value NULL is equal to zero
  • delete intptrintptr NULL
  • this avoids accidentally dereferencing a
    dangling pointer
  • A pointer whose value is NULL is a null pointer
  • we often use a ground symbol to denote a NULL
    pointer

12
  • How to avoid dangling pointers
  • release memory only when there is only one
    pointer to it!
  • int intPtr1int intPtr2
  • intPtr1 new int
  • intPtr1 7
  • intPtr2 intPtr1
  • delete intPtr1
  • intPtr1 NULL

aliasing
Trouble!
13
Memory leaks If there is no pointer pointing to a
dynamically allocated block of memory, there is a
memory leak int intPtr intPtr new
int intPtr 7 intPtr new int intPtr
5
14
  • Invoking delete with and without the
  • What memory is released when a pointer points to
    an array?
  • object data new object10
  • delete data
  • delete operator releases all 10 objects in
    memory
  • object data new object10
  • delete data
  • delete operator might release all 10 objects in
    memory
  • but only one destructor is invoked (for the
    first object)
  • The difference is whether 1 or 10 destructors are
    called

15
  • More about delete with and without the
  • The C standard says to use the consistently
  • object data new object
  • delete data
  • But for arrays
  • object data new object10
  • delete data
  • New compilers require that you do this (more
    later)

16
  • Garbage collection in Java
  • Java has automatic, or implicit garbage
    collection.
  • Many of the concerns that arise in C do not
    occur in Java because of this
  • no dangling pointers
  • no memory leaks
  • no explicit delete operations
  • no memory fragmentation
  • no hassles remembering arrays vs. scalars
    (non-arrays)
Write a Comment
User Comments (0)
About PowerShow.com