copyright © 2006 pearson addison-wesley. all rights reserved. 10-1 this weeks topics: pointers...

19
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 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

Upload: felix-stafford

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1

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

Page 2: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-2

Modify C-String in a function call or exchange the value of two C-Strings 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?

Page 3: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-3

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

Page 4: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-4

Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)

Page 5: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-5

Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (2 of 2)

Page 6: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-6

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

Page 7: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-7

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 bypointer p"

Literally "destroys" memory

Page 8: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-8

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

Page 9: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-9

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!

Page 10: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-10

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!

Page 11: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-11

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

Page 12: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-12

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] << " " ;

Page 13: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Array and Pointers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-13

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;

}

Page 14: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Array and Pointers (continued)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-14

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;

}

Page 15: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-15

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

Page 16: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-16

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

Page 17: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-17

Deleting Dynamic Arrays

Allocated dynamically at run-time So should be destroyed at run-time

Simple again. Recall Example:double * d = new double[10];… //Processingdelete [] d; De-allocates all memory for dynamic array Brackets indicate "array" is there

Page 18: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-18

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!

Page 19: Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 This Weeks Topics: Pointers (continued)  Modify C-String through a function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-19

Multidimensional Dynamic Arrays: De-allocate memories In the reverse order allocating spaces

for (int i = 0; i < 3; i++)delete [] m[i];

delete[] m;