Example: air traffic controller

# Chapter 10. 포인터와동적배열 - parkjonghyuk.net

SeoulTech 2011-2nd (2). Chapter 10.. UCS Lab ( ). Tel: 970-6702. Email: Learning Objectives .. , , . this . , . UCS Lab 10-2.. : .. ! Call-by-reference .. UCS Lab 10-3.. Int double . ! : . double *p; ! p double . double . !! UCS Lab 10-4.. "*" . "*" . int *p1, *p2, v1, v2;. p1, p2 . v1, v2 . UCS Lab 10-5.. , ! (abstraction)! C++ . , .. UCS Lab 10-6. (1/3).. , .. , .. UCS Lab 10-7. (2/3). int *p1, *p2, v1, v2;. p1 = . p1 v1 . & .. : p1 v1 . p1 v1 . UCS Lab 10-8. (3/3). int *p1, *p2, v1, v2;. p1 = . v1 : v1 : cout << v1;. p1 : cout << *p1;. (dereference) , *. : p1 . UCS Lab 10-9.. : v1 = 0;. p1 = . *p1 = 42;. cout << v1 << endl;. cout << *p1 << endl;. : 42. 42. p1 v1 . UCS Lab 10-10. & .. call-by-reference . ! call-by-reference .. call-by-reference .. UCS Lab 10-11.. : int *p1, *p2;. p2 = p1;. p1 p2 . : *p1 = *p2;. p2 p1 .. UCS Lab 10-12.. 4-13. new .. new .. ! p1 = new int;. p1 . *p1 .. UCS Lab 10-14. (1/2). 4-15. (2/2). 4-16.

UCS Lab 포인터 개요 •포인터 정의: –변수의 메모리 주소 •메모리 분할 –넘버링 된 메모리 위치 –변수를 위해 사용된 메모리 주소

### Information

Domain:

Source:

Please notify us if you found a problem with this document:

### Transcription of Chapter 10. 포인터와동적배열 - parkjonghyuk.net

1 SeoulTech 2011-2nd (2). Chapter 10.. UCS Lab ( ). Tel: 970-6702. Email: Learning Objectives .. , , . this . , . UCS Lab 10-2.. : .. ! Call-by-reference .. UCS Lab 10-3.. Int double . ! : . double *p; ! p double . double . !! UCS Lab 10-4.. "*" . "*" . int *p1, *p2, v1, v2;. p1, p2 . v1, v2 . UCS Lab 10-5.. , ! (abstraction)! C++ . , .. UCS Lab 10-6. (1/3).. , .. , .. UCS Lab 10-7. (2/3). int *p1, *p2, v1, v2;. p1 = . p1 v1 . & .. : p1 v1 . p1 v1 . UCS Lab 10-8. (3/3). int *p1, *p2, v1, v2;. p1 = . v1 : v1 : cout << v1;. p1 : cout << *p1;. (dereference) , *. : p1 . UCS Lab 10-9.. : v1 = 0;. p1 = . *p1 = 42;. cout << v1 << endl;. cout << *p1 << endl;. : 42. 42. p1 v1 . UCS Lab 10-10. & .. call-by-reference . ! call-by-reference .. call-by-reference .. UCS Lab 10-11.. : int *p1, *p2;. p2 = p1;. p1 p2 . : *p1 = *p2;. p2 p1 .. UCS Lab 10-12.. 4-13. new .. new .. ! p1 = new int;. p1 . *p1 .. UCS Lab 10-14. (1/2). 4-15. (2/2). 4-16.

2 4-17. new - . new , .. : . : MyClass *mcPtr;. mcPtr = new MyClass( , 17);. : int *n;. n = new int(17); //Initializes *n to 17. UCS Lab 10-18.. : int* findOtherPointer(int* p);. UCS Lab 10-19.. (Heap). (freestore).. new . UCS Lab 10-20.. : int *p;. p = new int;. if (p == NULL). {. cout << "Error: Insufficient memory.\n";. exit(1);. }. new .. UCS Lab 10-21.. : new : .. null . UCS Lab 10-22.. UCS Lab 10-23. delete .. , . : int *p;. p = new int(5);. //Some processing . delete p;. p . UCS Lab 10-24. (Dangling Pointer). delete p;.. p ! (dangling pointer) . *p . ! . delete null : delete p;. p = NULL;. UCS Lab 10-25.. (Dynamic variable). new .. (Local variable).. UCS Lab 10-26.. typedef int* IntPtr;.. IntPtr p;. int *p;.. UCS Lab 10-27. : Call-by-value .. , ??? UCS Lab 10-28. Call-by-Value . (1/2). 4-29. Call-by-Value . (2/2). 4-30. sneaky(p);. 4-31.. ! .. UCS Lab 10-32.. ! Example: int a[10];. int * p;. a p ! UCS Lab 10-33. (1/2).

3 Int a[10];. typedef int* IntPtr;. IntPtr p;. a p are pointer variables : p = a; // Legal. p a now points where a points a . a = p; // ILLEGAL! ! UCS Lab 10-34. (2/2).. int a[10];. a . "int * const" type . a ! ! . UCS Lab 10-35.. : . : .. UCS Lab 10-36.. ! new .. : typedef double * DoublePtr;. DoublePtr d;. d = new double[10]; //Size in brackets UCS Lab 10-37.. , . : d = new double[10];. //Processing delete [ ] d;.. [ ] . d ! d = NULL;. UCS Lab 10-38.. : int [ ] someFunction(); // ILLEGAL! : int* someFunction(); // LEGAL! UCS Lab 10-39.. : typedef double* DoublePtr;. DoublePtr d;. d = new double[10];. d d[0] . d + 1 d[1] . d + 2 d[2] . UCS Lab 10-40.. ! : for (int i = 0; i < arraySize; i++). cout << *(d + I) << " " ;. : for (int i = 0; i < arraySize; i++). cout << d[I] << " " ;. +/- . *, / . ++ -- . UCS Lab 10-41.. typedef int* IntArrayPtr;. IntArrayPtr *m = new IntArrayPtr[3];.. 4 . for (int i = 0; i < 3; i++). m[i] = new int[4].

4 3*4 ! UCS Lab 10-42.. -> . * dot(.) . ( ). : MyClass *p;. p = new MyClass;. p->grade = "A"; Equivalent to: (*p).grade = "A";. UCS Lab 10-43. this . , . this . : Class Simple {. public: void showStuff() const;. private: int stuff;. };. : cout << stuff;. cout << this->stuff;. : return *this UCS Lab 10-44. (self-reference).. this .. UCS Lab 10-45. (self-reference). this . ! UCS Lab 10-46. (1). #include <iostream>. using std::cout;. using std::endl;. class Person {. public: Person* GetThis(){. return this; //this . }. };. int main(). {. cout<<"** p1 **"<<endl;. Person *p1 = new Person();. cout<<" p1: "<<p1<<endl;. cout<<"p1 this: "<<p1->GetThis()<<endl;. cout<<"** p2 **"<<endl;. Person *p2 = new Person();. cout<<" p2: "<<p2<<endl;. cout<<"p2 this: "<<p2->GetThis()<<endl;. return 0;. } UCS Lab 10-47. (2). #include <iostream>. using std::cout; using std::endl;. class Data {. int aaa;. int bbb;. public : Data(int aaa, int bbb) {.}}

5 //aaa=aaa;. this->aaa=aaa;. //bbb=bbb;. this->bbb=bbb;. }. void printAll() { cout<<aaa<<" "<<bbb<<endl;. }. };. int main(void){. Data d(100, 200);. ();. return 0;. } UCS Lab 10-48. (3). #include <iostream> void Dog::make_tail(char *s). using std::cout; {. using std::endl; tail = new Dog(s); // tail . tail->body = this;. class Dog { // . Dog *body; }. char *name;. Dog *tail; char *Dog::tail_name(). public: {. Dog(char *s); return tail->dog_name();. char *dog_name(); }. void make_tail(char *s);. char *tail_name(); int main(). }; {. Dog dog("Happy");. Dog::Dog(char *s). { ("Merry");. name = new char[strlen(s) + 1]; cout << "dog name : " << (). << endl;. strcpy(name, s);. cout << "tail name : " << (). tail = body = 0; << endl;. }. char *Dog::dog_name() return 0;. { return name; } }. UCS Lab 10-49. (3). dog name : Happy tail name : Merry body, tail . make_tail() . tail body this .. body name tail dog Happy . body name tail Merry.

6 UCS Lab 10-50. (1/2).. , . ) a = b = c;.. this ! UCS Lab 10-51. (2/2).. s1 = s2;. (s2);. s1 = s2 = s3;. s1 = (s2 = s3);.. UCS Lab 10-52. = (1/3). : class StringClass {. public: . void someProcessing();.. StringClass& operator=(const StringClass .. private: char *a; // . int capacity; // a . int length; // a . };. UCS Lab 10-53. = (2/3). ( ): StringClass& StringClass::operator=(const StringClass& rtSide). {. capacity = ;. length = ;. delete [] a;. a = new char[capacity]; s = s;. for (int I = 0; I < length; I++) delete [] a;. a[I] = [I]; delete [] ;. return *this;. }. UCS Lab 10-54. = (3/3). ( ): StringClass& StringClass::operator=(const StringClass& rtSide). {. if (this == &rtSide) // if right side same as left side return *this;. else {. capacity = ;. length length = ;. delete [] a;. a = new char[capacity];. for (int I = 0; I < length; I++). a[I] = [I];. return *this;. }. }. UCS Lab 10-55. (Shallow Copy) . (Deep Copy).

7 (Shallow copy).. (Deep copy). , .. ! UCS Lab 10-56.. delete . private .. : ! UCS Lab 10-57.. , .. , ~ . MyClass::~MyClass(). {. //Perform delete clean-up duties }. UCS Lab 10-58. (Copy Constructor). : 1.. 2.. 3. , call-by-value .. "=", member-wise copy ! UCS Lab 10-59. 1.. (Freestore).. UCS Lab 10-60. 2.. UCS Lab 10-61. Q&A. UCS Lab 4-62.