190 likes | 208 Views
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.
E N D
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
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?
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, andassigns p1 to "point to" it • Can access with *p1 • Use just like ordinary variable
Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)
Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (2 of 2)
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
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
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
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!
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!
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
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] << " " ;
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; }
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; }
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
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
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
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!
Multidimensional Dynamic Arrays: De-allocate memories • In the reverse order allocating spaces for (int i = 0; i < 3; i++) delete [] m[i]; delete[] m;