110 likes | 211 Views
CS31: Introduction to Computer Science I. Discussion 1A 5/21/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week Pointers variables that store address (memory location) pointers hold the address of the same type of variables
E N D
CS31: Introduction to Computer Science I Discussion 1A 5/21/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang
Quick Review • What did we learn last week • Pointers • variables that store address (memory location) • pointers hold the address of the same type of variables • int type pointer only stores the address of int variable • double type pointer only stores the address of double variable • * used to declare pointer variables • & used to get the address of variables • * (dereferencing operator) used to get the value of the memory location • multiple pointers can point to the same location • Pointers and Arrays • The name of an array is a pointer to its first element(item) • can access all the array items with arithmetic operations • Pointers and Functions • pointers can be passed into functions either by value or by reference • function can return pointer
Dynamic memory allocation • The compiler manages an area of memory called the heap for allocation by request • We can request memory space “on-the-fly” or “dynamically” from the heap area using the new operator • The return value of the new operator is the address of the memory allocated • value must be saved in a pointer • The pointer is used for data access
Dynamic Array Declaration • array declaration requires specific size(length) • new operator allows dynamic creation of an array int main () { int len; cin >> len; int arr[len]; //ERROR } int main () { int len; cin >> len; int *arr = new int[len]; } size of len int array will be created arr stores the address of first array element
quick question • what will be displayed? int main () { int len = 3, *ptr = &len; int *arr = new int[len]; *arr = *ptr; *(arr+1) = 20; *(arr+2) = *arr + *(arr+1); for (int i=0; i<len; i++) cout << *(arr+i) << endl; } 3 20 23
creation & destruction • regular variables • created statically • destroyed automatically • dynamic variables • created dynamically with new • destroyed manually with delete • need to destroy variables before program terminates • otherwise, it will cause memory leakage
memory leak • dynamic variables do not have names • can refer to them only by pointers • what will happen if we lose the pointers? int main () { int *ptr; ptr = new int[100]; … … ptr = new int[10]; … … delete[] ptr; }
Dynamic allocation Dynamic Allocation Failure • If the dynamic memory allocation fails, the return value from new will be NULL • The value can be tested after using new to ensure success • The constant NULL is defined in iostream • The actual value of NULL is zero
Questions • Do you see any problem? • it compiles and runs, but it causes warning message and it does not guarantee what we expect. int *func() { int *arr = new int[5]; for (int i=0 ; i<5 ; i++) { *(arr+i) = i; } return arr; } int main() { int *ptr = func(); for (int i=0 ; i<5 ; i++) { cout << *(ptr+i) << endl; } delete[] ptr; } int *func() { int arr[5]={0,1,2,3,4}; return arr; } int main() { int *ptr = func(); for (int i=0 ; i<5 ; i++) { cout << *(ptr+i) << endl; } }
Questions • Do you see any problem? • it compiles and runs, but it causes warning message. int main() { int *ptr[10]; for(int i=0 ; i<10 ; i++) { ptr[i] = new int; *ptr[i] = i; cout << *ptr[i] << endl; } for(int i=0 ; i<10 ; i++) { delete ptr[i]; } } int main() { int *ptr[10]; for(int i=0 ; i<10 ; i++) { ptr[i] = new int; *ptr[i] = i; cout << *ptr[i] << endl; } delete[] ptr; }
Questions • what will be displayed? void func(int *p) { *p = 11; *(p+1) = 12; p += 3; *(p+1) = 15; } int main() { int arr[5] = {1,2,3,5,8}; func(arr); for(int i=0 ; i<5 ; i++) cout << arr[i] << endl; } void func(int *p) { for (int i=0 ; i<5 ; i++) { *(p+i) = i; } } int main() { int *arr = new int[5]; func(arr); for(int i=0 ; i<5 ; i++) cout << arr[i] << endl; } 11 12 3 5 15 0 1 2 3 4