1 / 18

This Weeks Topics: Pointers continued

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' workPass by the reference of the c-string (char *

thea
Télécharger la présentation

This Weeks Topics: Pointers continued

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    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

    2. Modify C-String in a function call or exchange the value of two C-Strings Pass by value of the c-string (char *)? Wont 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?

    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

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

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

    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

    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 by pointer p" Literally "destroys" memory

    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

    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!

    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!

    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

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

    13. Array and Pointers

    14. Array and Pointers (continued)

    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

    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

    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]; //Processing delete [] d; De-allocates all memory for dynamic array Brackets indicate "array" is there

    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!

    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;

More Related