Dynamic Memory Allocation - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Dynamic Memory Allocation

Description:

... can be used for dynamic (vs. compile time, or static) allocation of memory ... A reference type is like a 'constant pointer' ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 18
Provided by: susanb69
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Allocation


1
Dynamic Memory Allocation
2
Dynamic memory allocation
  • In addition to aliases for existing variables,
    pointers can be used for dynamic (vs. compile
    time, or static) allocation of memory
  • Dynamic allocation is useful when
  • objects need to be created and the constructor
    arguments are not known until run time
  • arrays need to be created whose extent is not
    known until run time
  • complex structures of unknown size and/or shape
    need to be constructed as the program runs

3
new and delete
  • To dynamically allocate space, use new
  • To free this space later, use delete
  • Examples

int ip ip new int ip 22 cout ltlt ip ltlt
endl delete ip ip NULL
4
Class Example
  • class Trader
  • private
  • string name
  • float money
  • public
  • Trader(string n, float m)
  • float getMoney()
  • string getName()
  • .

5
Dynamic Allocation of an Object
  • Trader tptr
  • string n
  • float m
  • cin gtgt n gtgt m
  • tptr new Trader(n,m)
  • cout ltlt (tptr).getName()
  • cout ltlt (tptr).getMoney()
  • delete tptr
  • tptr NULL

6
Memory leaks
  • When you dynamically create objects, the only way
    you have of accessing them is through the pointer
    which is assigned by the new statement
  • Reassigning a pointer without deleting the memory
    it pointed to previously is called a memory leak

7
Memory leak example
char cp NULL char cq NULL cp new
char(B) cq new char cq cp // Memory
cq // pointed to is no
// longer accesible
cp
B
cq
8
Dynamic creation of arrays
  • Use the dim on the new statement to create an
    array of objects instead of a single instance.
  • On the delete statement use to indicate that
    an array of objects is to be deleted.

9
Example of dynamic array allocation
int grades NULL int numberOfGrades cout ltlt
Enter the number of grades cin gtgt
numberOfGrades grades new intnumberOfGrades
for (int i 0 i lt numberOfGrades i)
cin gtgt gradesi Sort (grades,
numberOfGrades) for (int j 0 j lt
numberOfGrades j) cout ltlt
gradesj delete grades grades NULL
10
Reference types
  • A reference type is like a constant pointer
  • A reference type is given a value when it is
    declared (except in formal arguments to a
    function)
  • A reference value cannot be changed once declared
  • Function arguments to be modified should be
    formally declared as reference type

11
Reference types
int myValue int myAlias myValue myAlias
20 // assigns 20 to myValue cout ltlt
myValue // outputs the value 20 double sum,
average double total sum double value
// error must be initialized double total
average // error cannot reassign total
12
Pointers as arguments to functions
  • Pointers can be passed to functions just as other
    types can
  • Just as with any other argument, verify that the
    number and type of arguments in function
    invocation match the prototype (and function
    header)

13
Example of pointer arguments
int MyFunction(int p, int i) // prototype for
MyFunction void main ( ) int r, s, t
int tp t r MyFunction(tp, s) //
passes int and int variables r
MyFunction(t, s) // passes address of int
and int r MyFunction(tp, tp) // passes
int and contents of int int MyFunction(int
p, int i) p 3 i 4 return i
14
Pointers vs. ReferencesPointer argument example
void accumulate (int addend, int
total) include ltiostream.hgt void main()
int sumValue 3 int newAmount 4
int sum sumValue accumulate (newAmount,
sum) cout ltlt sum ltlt endl void accumulate
(int addend, int total) total total
addend return
15
Pointers vs. ReferencesPointer argument example
main
Accumulate
newAmount 4 sum sumValue sumValue
3 7
addend 4 total sumValue
4
sumValue
7
16
Pointers vs. ReferencesReference argument
example
void accumulate (int addend, int
total) include ltiostream.hgt void main()
int sum 3 int newAmount 4
accumulate (newAmount, sum) cout ltlt sum ltlt
endl void accumulate (int addend, int
total) total total addend return

17
Pointers vs. ReferencesReference argument
example
main
Accumulate
newAmount 4 sum 3 7
addend 4 total sum
4
sum
7
Note Returning values via arguments should be
avoided. Whenever possible, functions should be
designed to return a single object (which may be
a complex data structure).
Write a Comment
User Comments (0)
About PowerShow.com