Transcript Lecture 04

This Weeks Topics: Pointers
(continued)
 Modify C-String through a function call
 Dynamic variable
 new keyword and delete keyword
 Arrays and pointers
 Pointer arithmetic
 Dynamic array
 Size not specified at programming time
 Determined while program running
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-1
Modify C-String in a function call
or exchange the value of two CStrings
 Pass by value of the c-string (char *)?
 Won’t’ work
 Pass by the reference of the c-string (char * &)?
 Yes, it works
 Pass by pointer to the c-string (char **)?
 Yes, it works
 How to implement?
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-2
The new Operator
 Since pointers can refer to variables…
 No "real" need to have a standard variable
 Can dynamically allocate variables
 Operator new creates variables
int * p1;
p1 = new int;
 Creates new "nameless" variable, and
assigns p1 to "point to" it
 Can access with *p1
 Use just like ordinary variable
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-3
Basic Pointer Manipulations Example:
Display 10.2 Basic Pointer
Manipulations (1 of 2)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-4
Basic Pointer Manipulations Example:
Display 10.2 Basic Pointer
Manipulations (2 of 2)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-5
Memory Management
 Heap
 Reserved for dynamically-allocated variables
 All new dynamic variables consume heap memory
 If too many  could use all heap memory
 Problem: future "new" operations will fail if
heap is "full“
 Resolution: use delete operator to de-allocate
spaces
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-6
delete Operator
 De-allocate dynamic memory
 When no longer needed
 Returns memory to heap
 Example:
int *p;
p = new int;
… //Some processing…
delete p;
 De-allocates dynamic memory "pointed to by
pointer p"
 Literally "destroys" memory
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-7
Dynamic and Regular Variables
 Dynamic variables
 Created with new operator; destroyed with
delete operator
 Created and destroyed while program runs
 Local variables
 Declared within function definition
 Not dynamic
 Created when function is called
 Destroyed when function call completes
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-8
Recall: Array Variables
 Arrays stored in memory addresses
sequentially
 Array variable "refers to" first indexed
variable
 So array variable is a kind of pointer
variable!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-9
Array Variables  Pointers
 Example:
int a[10];
int * p;
 and p are pointer variables
 Can perform assignments:
p = a; // Legal.
 p now points where a points
 To first indexed variable of array a
 a = p; // ILLEGAL!
 Array pointer is CONSTANT pointer!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-10
Pointer Arithmetic
 Can perform arithmetic on pointers
 "Address" arithmetic
 Example:
double f[4] = {1.1, 2.2, 3.3, 4.4};
double * d = f;
 d contains address of d[0]
 d + 1 evaluates to address of d[1]
 d + 2 evaluates to address of d[2]
 Equates to "address" at these locations
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-11
Alternative Array Manipulation
 Use pointer arithmetic!
 “Step through” array without indexing:
for (int i = 0; i < arraySize; i++)
cout << *(d + i) << " " ;
 Equivalent to:
for (int i = 0; i < arraySize; i++)
cout << d[i] << " " ;
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-12
Array and Pointers
 Array can be accessed using pointers; passing array as a
parameter in a function is passing the address of the first
element
int main()
{
int n[4]={0,0,0,0};
int *p;
p=&n[0]; // same as p =n;
*p = 1; // same as p[0] = 1;
*(p+1) = 1 // same as p[1] = 1;
for (int i=0; i<4; i++) cout << n[i] << " ";
return 0;
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-13
Array and Pointers (continued)
C-String is a special character array
int main()
{
char s[] = "Hi There!";
char *p;
p=&s[0]; // same as p = s;
*p = 'B'; // what will happen here?
*(p+1) = ‘C’; // what will happen here?
p = "Bye Bye!";
cout << s << endl;
return 0;
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-14
Dynamic Arrays
 Array limitations
 Must specify size first
 May not know until program runs!
 Must "estimate" maximum size needed
 Sometimes OK, sometimes not
 "Wastes" memory
 Dynamic arrays
 Can determine size at runtime
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-15
Creating Dynamic Arrays
 Use new operator
 Dynamically allocate with pointer variable
 Treat like standard arrays
 Example:
double *d;
d = new double[10]; //Size in brackets, can
be variable
 Creates dynamically allocated array
variable d,with ten elements, base type
double
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-16
Deleting Dynamic Arrays
 Allocated dynamically at run-time
 So should be destroyed at run-time
 Simple again. Recall Example:
double * d = new double[10];
… //Processing
delete [] d;
 De-allocates all memory for dynamic array
 Brackets indicate "array" is there
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-17
Multidimensional Dynamic Arrays
 Recall: "arrays of arrays"
int **m = new int *[3];
 Creates array of three pointers
 Make each allocate array of 4 ints
for (int i = 0; i < 3; i++)
m[i] = new int[4];
 Results in three-by-four dynamic array!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-18
Multidimensional Dynamic
Arrays: De-allocate memories
 In the reverse order allocating spaces
for (int i = 0; i < 3; i++)
delete [] m[i];
delete[] m;
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-19