Title: Dynamic Storage and Arrays
1Dynamic Storage and Arrays
- Computer Programming I
- Dr. Tim Margush
- Mathematics, Science and Technology Department
- Farquhar Center - Nova Southeastern University
2Storage Classes
- Static storage
- Lifetime program's lifetime
- Automatic Storage
- Lifetime variable's lifetime (while in scope)
- Stack
- Dynamic Storage
- Lifetime is under programmer's control
- Heap
3Dynamic Storage
- Programmer allocated
- int iPtr new int
- double dPtr new double
- BankAccount ba new BankAccount("Fred")
- Programmer released
- delete iPtr
- delete dPtr
- delete ba
4new and delete
- new allocates a block of storage from the heap
- The size of the block is determined by the
argument of new which must be a data type - delete releases a block of storage to the heap
manager - The argument, a pointer, specifies the address of
the block
5Memory Leaks
- Each allocation (via new) must be matched to a
release (via delete) - Failure to do this results in a memory leak
- Memory leaks consume system resources and can
lead to system failure
6Example Dynamic Storage Allocation
- //create a pointer variable
- int iPtr //automatic storage class
- //allocate storage for an int
- iPtr new int //heap storage
- iPtr 27 //initialize the int in heap
- //release the storage
- delete iPtr //storage returned to heap
iPtr
Allocated from stack area
???
7Example Dynamic Storage Allocation
- //create a pointer variable
- int iPtr //automatic storage class
- //allocate storage for an int
- iPtr new int //heap storage
- iPtr 27 //initialize the int in heap
- //release the storage
- delete iPtr //storage returned to heap
iPtr
Allocated from heap area
???
8Example Dynamic Storage Allocation
- //create a pointer variable
- int iPtr //automatic storage class
- //allocate storage for an int
- iPtr new int //heap storage
- iPtr 27 //initialize the int in heap
- //release the storage
- delete iPtr //storage returned to heap
iPtr
iPtr is an l-value that provides access to the
int in the heap
27
9Example Dynamic Storage Allocation
- //create a pointer variable
- int iPtr //automatic storage class
- //allocate storage for an int
- iPtr new int //heap storage
- iPtr 27 //initialize the int in heap
- //release the storage
- delete iPtr //storage returned to heap
iPtr
This memory is marked as unused
27
iPtr is now invalid
10Arrays
- An array is a homogeneous collection of storage
locations arranged consecutively in memory - Each storage location is called a cell
- Each cell contains a value of the same data type
- The size of each cell is the same
- Cells are numbered 0, 1, 2, 3, ...
11Arrays and Vectors
- The implementation of the vector class uses
arrays as the underlying storage arrangement - Vectors are objects - Available in C only
- Vectors have properties and methods defined in
the vector class - Arrays are a standard C language construct
- Some C operators can be applied to arrays
12Declaring Arrays
- int x20
- Declares and creates an array x in the stack with
20 integers (initially undefined) - char s12
- Declares and creates an array s in the stack with
12 characters (initially undefined) - This is commonly referred to as a string
- This is a c-style string - not a string object
13The Type of an Array
- int x20
- The type of x is int
- char s12
- The type of s is char
- An array variable is considered a pointer to the
base-type of the array - It points to the first cell of the array
14The Address of an Array
- int x20
- x represents the address of the array
- This is actually the address of the first cell of
the array - the base-address of the array - x is of type int (note so is x)
- x and x are considered the same in the C
language - "An array is a pointer"
15Accessing Cells of an Array
- int x20
- The operator is commonly used to access array
cells - x0 accesses cell 0 (the first cell) of the
array - x19 accesses the last cell of the array
- xi 12 stores a 12 in the specified cell
- Assuming i is between 0 and 19
- cout ltlt xi displays an element of the array
16Accessing Cells of an Array
- int x20
- Since x is a pointer to a cell (type int )
- x accesses cell 0 (the first cell) of the array
- (x19) accesses the last cell of the array
- Note x19 is not the same due to operator
precedence - x 12 stores a 12 in x0
- cout ltlt x displays the first cell of the array
17Arrays and Pointers
- char s12
- char sPtr s
- Both s and sPtr can be considered arrays
- sPtr is actually a pointer to an array
- Both s and sPtr can be considered pointers
- Each point to a char (one character or byte)
- sPtr can be changed - s cannot
18Arrays and Pointers
- char s12
- char sPtr s
- The array name, s, is called a const pointer
- It cannot be changed
- It is a special const pointer as the address of
the array is not actually stored in memory - sPtr is the name of a pointer variable
- Storage for the pointer is in the stack
19Arrays and Pointers
Storage for the pointer is allocated
s
sPtr
Both storage areas are of the automatic storage
class (in the stack)
20Arrays and Pointers
sPtr's contents can be changed to point wherever
you want
- char s12
- char sPtr s
- sPtr7
s
sPtr
Even though s is a pointer to the array, it has
no storage allocated for it s cannot be changed
21Automatic Arrays
- int x20
- Arrays declared in this way are called automatic
arrays - The space for the array is automatically
allocated (in the stack) when the array comes
into scope - This space is automatically released when the
array goes out of scope
22Dynamic Arrays
- Arrays are allocated in the heap are called
dynamic (or dynamically allocated) arrays - Dynamic arrays have no name
- Dynamic arrays are referred to by address
- The new operator is used to allocate dynamic
arrays - int xPtr new int20
- char sPtr new char12
23Automatic vs Dynamic Arrays
- int x20
- for (i0ilt20i)
- xi i1
- cout ltlt (x4)
- int xPtrnew int20
- for(i0ilt20i)
- xPtri i1
- cout ltlt (xPtr4)
Once created, automatic and dynamic arrays can be
used in exactly the same way. In C, an array is
a pointer and a pointer is an array
24Automatic vs Dynamic Arrays
- int x20
- for (i0ilt20i)
- xi i1
- cout ltlt (x4)
- int xPtrnew int20
- for(i0ilt20i)
- xPtri i1
- cout ltlt (xPtr4)
- delete xPtr
One important difference - dynamic allocation
requires a programmed action to release the
storage when it is no longer needed!
25Releasing Array Storage
- Storage allocated using new must be released
using delete - delete and delete look deceivingly similar
- Each requires only a pointer as an argument
- delete only releases one component of the type
pointed to by the argument - delete releases an array of components starting
with the one pointed to by the argument
26Releasing Array Storage
- int oneEyePtr new int
- int twoEyePtr new int2
- ...
- delete oneEyePtr
- delete twoEyePtr
The heap manager knows the size of the array that
was originally allocated
27Array Processing With Pointers
- int x5, i
- int xPtr x
- for (i0 ilt5 i)
- xPtr ii
- xPtr
x
xPtr
?
?
?
?
?
28Array Processing With Pointers
- int x5, i
- int xPtr x
- for (i0 ilt5 i)
- xPtr ii
- xPtr
x
xPtr
0
1
?
?
?
29Array Processing With Pointers
- int x5, i
- int xPtr x
- for (i0 ilt5 i)
- xPtr ii
- xPtr
x
xPtr
0
1
4
?
?
30Array Processing With Pointers
- int x5, i
- int xPtr x
- for (i0 ilt5 i)
- xPtr ii
- xPtr
x
xPtr
?
0
1
4
9
16
31Arrays and Assignment
- The assignment operator does not work with arrays
- int x10, y10
- x y //illegal
- If the intent was to copy the cells of y into the
cells of x, a loop is required - for(i0 ilt10 i) xiyi
32C-Style Strings
- All of the following create and initialize
variable that may be called a string - char s5
- char sPtr s
- char dsPtr new char5
- None of the string values have yet been defined,
only the storage for the strings
33C-Style Strings
- Storing string values requires array processing
- s0'T' s1'i' s2'm' s3'\0'
- All c-style strings must be terminated with a
null character - '\0' - the character with ASCII code of 0
- The null character is a delimiter - anything
found after the null is considered garbage
34Special Treatment
- C recognizes the pointer type char as special
- char s5
- cin gtgt s
- Causes the next 'string token' to be extracted
into the array s (hope it fits) - a null is
appended - cout ltlt s
- Displays the characters in s preceeding the first
null
35String Literals
- "Tim" is a string literal (c-style)
- It represents the array of 4 characters'T' 'i'
'm' '\0' - Its type is char
- Pointers to strings are sometimes initialized to
point to string literals - char myName "Tim"
- char sPtr "This is a c-style string"
36String Literals
- char x10
- x "Tim"
- Although this looks legal, it is not!
- Remember that arrays are constant pointers
- This assignment is an attempt to change where x
"points" - C provides a powerful library to manipulate
strings
37include ltstring.hgt
- The string.h library provides a utility function
to copy strings - char s10
- strcpy(s,"Tim")
- The string.h library provides a utility function
to compare strings - if (strcmp(s,"tim") 0) cout ltlt "equal"