Title: Dynamic Memory Allocation
1Dynamic Memory Allocation
2Dynamic 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
3new 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
4Class Example
- class Trader
-
- private
- string name
- float money
- public
- Trader(string n, float m)
- float getMoney()
- string getName()
- .
5Dynamic 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
6Memory 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
7Memory 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
8Dynamic 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.
9Example 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
10Reference 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
11Reference 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
12Pointers 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)
13Example 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
14Pointers 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
15Pointers vs. ReferencesPointer argument example
main
Accumulate
newAmount 4 sum sumValue sumValue
3 7
addend 4 total sumValue
4
sumValue
7
16Pointers 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
17Pointers 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).